Wei Yan was from Yiyang Commandery (義陽郡; around present-day Tongbai County, Henan) in Jing Province. As one of Liu Bei's personal retainers, he followed Liu Bei into Yi Province (covering present-day Sichuan and Chongqing) in 212 to attack the warlord Liu Zhang. He had many military accomplishments and was assigned as a General of the Standard (牙門將軍). When Liu Bei became King of Hanzhong in 219 after his victory over his rival Cao Cao in the Hanzhong Campaign, he selected Wei Yan to be in charge of guarding Hanzhong Commandery and appointed him General Who Guards the North (鎮北將軍). This was done in spite of the fact that most of Liu Bei's officers believed that Zhang Fei would be the most likely candidate for the position, which assumed the imminent military responsibility of defending Hanzhong from attacks by Cao Cao. When Liu Bei announced his decision to entrust Wei Yan with that responsibility, his other subordinates were surprised. Liu Bei then asked Wei Yan in front of everyone how would he perform his duty, to which Wei Yan confidently replied, "If Cao Cao mustered all his forces and invaded, let me assist my lord to repel the enemies; if an Assistant-General came with 100,000 troops, let me engulf them for my lord." During his tenure, Wei Yan borrowed the concept of "double gates" from the ancient text Zhou Yi and laid numerous camps along the outskirt and trail exits linking to Hanzhong. His defence mechanism was very effective in driving the enemy out, and his arrangement would be adopted by those who later succeeded his post.
In 221, Liu Bei founded the state of Shu Han and became its first emperor. He died in 223 and was succeeded by his son, Liu Shan. In that year, Liu Shan made Wei Yan a Marquis of a Chief Village (都亭侯). About four years later, Shu's chancellor-regent Zhuge Liang launched the first of a series of campaigns to attack Shu's rival state Cao Wei, and Wei Yan was called into battle. Wei Yan supervised the front division, and he held the appointments of Major under the Chancellor (丞相司馬) and Inspector of Liang Province (涼州刺史). Wei Yan treated his soldiers well and was known for his bravery. However, he was also boastful of his talents and his peers tended to avoid him. Yang Yi, a close aide of Zhuge Liang, made no concession to Wei Yan, and Wei Yan was extremely resentful of him.
Wei Yan participated in most – if not all – of the Northern Expeditions against the state of Cao Wei. However, he was disappointed with Zhuge Liang because the latter chose Ma Su to lead the vanguard for the first campaign, which ended in a total disaster after the Shu defeat at the Battle of Jieting. During the second campaign, Wei Yan suggested to Zhuge Liang to take an alternative route to attack Chang'an, a strategic city in Wei, but Zhuge Liang rejected his plan. Zhuge Liang then led the Shu army to besiege Chencang, where they could only lament upon the staunch defence of the 1,000 defenders led by the Wei general Hao Zhao.
In late 230, when the Cao Wei regime launched a counteroffensive with a sizeable army, Zhuge Liang sent Wei Yan and Wu Yi as military attachés to join forces with the Qiang tribes in the northwest to harass the Wei army's rear. Wei Yan swiftly led a mixed army of cavalry and infantry to his destination, where he sold the famous Chengdu silk brocades to the Qiang people in return for their military support, as well as for horses and weapons. From the very beginning, the Cao Wei attack encountered problems: heavy rains continued for more than 30 days, which rendered that the narrow valleys impassable, while the Wei general Zhang He was threatened in the west by Wei Yan's pincer movement towards his rear. After one and a half months of little progress, the ill-fated campaign was terminated. However, the Wei generals Fei Yao and Guo Huai wanted to earn some merits before their retreat, so they attacked Wei Yan at Yangxi (陽谿), but Wei Yan inflicted a crushing defeat on the enemy. Thus, the Shu force behind the enemy lines was able to make a prudence dictated return to Hanzhong Commandery. Wei Yan was then promoted to Vanguard Military Adviser (前軍師) and Senior General Who Attacks the West (征西大將軍), and enfeoffed as the Marquis of Nanzheng (南鄭侯).
During the fourth Northern Expedition, Zhuge Liang sent Wei Yan, Wu Ban and Gao Xiang to lead troops in three columns to counter the Wei commander Sima Yi, who was leading a frontal assault from the central main road. They resisted a Wei army personally led by Sima Yi outside the fortress and scored a major victory over the enemy, killing 3,000 Wei armoured soldiers and seizing 5,000 sets of armour and 3,100 crossbows.
Each time Wei Yan followed Zhuge Liang to battle, he would request to command a separate detachment of about 10,000 men and take a different route and rendezvous with Zhuge's main force at Tong Pass (present-day Tongguan County, Shaanxi), per an earlier strategy used by Han Xin during the Chu–Han Contention in the 200s BCE. Zhuge Liang rejected the plan, and Wei Yan felt that Zhuge was a coward and complained that his talent was not put to good use.
When Chen Shou compiled the unofficial works on the history of Shu to write the Sanguozhi, he only mentioned that Wei Yan suggested to Zhuge Liang to split the Shu army into two, and the two forces would take two different routes and rendezvous at Tong Pass.
Wei Yan's reasoning for his Ziwu Valley Plan was recorded in the historical text Weilüe, which was then added as annotations to his biography in the Sanguozhi. It stated: Wei Yan received intelligence that the defender of the strategic city Chang'an, Xiahou Mao, was cowardly and incompetent. Thus, he reasoned, it would be easy for him to take 5,000 troops (and another 5,000 to carry supplies) across the Qin Mountains via the Ziwu Valley (子午谷) and into Chang'an. Wei Yan estimated that he would reach Chang'an in ten days and scare Xiahou Mao into flight, leaving the grain in Chang'an's storehouses for Shu's taking. There, Wei Yan's force can wait for Zhuge Liang's main army to take the safer road out of Xie Valley (斜谷) and rendezvous in Chang'an. In this way, the region west of Xianyang could be conquered in one movement.
When the Cao Wei court received intelligence about Wei Yan's Ziwu Valley Plan, the emperor Cao Rui immediately removed Xiahou Mao from his military command in Chang'an and reassigned him to be a Master of Writing (尚書) in Luoyang.
In 234, Zhuge Liang launched the sixth northern expedition against Cao Wei, with Wei Yan leading the vanguard force. When he became critically ill during the stalemate at the Battle of Wuzhang Plains, he gave secret orders to Yang Yi, Fei Yi and Jiang Wei to lead the Shu army on a retreat back to Shu after his death, with Wei Yan in charge of the rearguard and Jiang Wei to follow behind. If Wei Yan refused to follow the order, they were to retreat without him. When Zhuge Liang died, news of his death were kept secret. Yang Yi sent Fei Yi to meet Wei Yan and assess his intentions.
Wei Yan said to Fei Yi, "Although the Chancellor (Zhuge Liang) is dead, I'm still around. The officials serving in the Chancellor's Office may bring his body back (to Chengdu) for burial, but I should remain behind to lead the army to attack the enemy. Must we abandon our mission just because of the death of one man? Besides, who am I, Wei Yan, to submit to Yang Yi's command and lead the rearguard?"
Wei Yan and Fei Yi then took command of the troops left behind. Fei Yi also wrote a letter, which was signed by both of them and would be read out to all the officers. He offered to return to the main camp and help Wei Yan explain the situation to Yang Yi. He also said that Yang Yi was a civil official who was less experienced in handling military affairs, so Wei Yan stood a chance of becoming the new commander. Wei Yan then let Fei Yi leave, but he immediately regretted his decision and went after Fei Yi, but could not catch up with him in time. He then sent his subordinate to meet Yang Yi and the others, but was shocked to discover that the various encampments were preparing to retreat in accordance with Zhuge Liang's final orders. Wei Yan wanted to continue the campaign against Cao Wei even though Zhuge Liang had died, so he turned furious when he heard of the retreat. He intended to block the Shu forces from retreating, so he led his force towards the south – ahead of the main army under Yang Yi's command – and sealed the return route by destroying the gallery roads leading back to Shu.
Wei Yan and Yang Yi separately wrote memorials to the Shu imperial court and accused each other of treason. Their memorials arrived in Chengdu on the same day. The Shu emperor Liu Shan asked the ministers Dong Yun and Jiang Wan for their opinions. Both of them sided with Yang Yi and felt that Wei Yan's actions were suspicious. In the meantime, Yang Yi ordered his men to cut down trees to rebuild the gallery roads, and his troops marched day and night to catch up with Wei Yan. Wei Yan arrived at the southern valley first and ordered his soldiers to attack Yang Yi. Yang Yi sent Wang Ping to resist Wei Yan. Wang Ping shouted at Wei Yan, "His lordship (Zhuge Liang) had just died and his body had yet to turn cold, and now you dare to do something like this!" Wei Yan's men knew that their commander was in the wrong so they deserted.
Wei Yan was left with only his son(s) and a few followers. They fled towards Hanzhong Commandery. Yang Yi ordered Ma Dai to give chase. Ma Dai caught up with Wei Yan, decapitated him, brought his head back, and threw it in front of Yang Yi. Yang Yi trampled on Wei Yan's head and said, "You inferior slave! Now, can you still commit evil?" Wei Yan's family members and close relatives were also executed. Before Wei Yan's death, Jiang Wan had led divisions of the imperial guards from Chengdu to deal with the conflict. They had travelled for about 10 li (about three miles) when they received news of Wei Yan's death, after which they returned to Chengdu.
Chen Shou, who wrote Wei Yan's biography in the Sanguozhi, analysed Wei Yan's death as such:
Wei Yan's initial intention was not to head north to surrender to Cao Wei. Instead, he wanted to retreat back to the south. He desired to kill Yang Yi and the others who disagreed with him. Although the officers held differing opinions, he strongly believed that they would generally agree to him becoming Zhuge Liang's successor. That was his true intention; he was not thinking of rebelling.
A similar, but somewhat different and more detailed account exists in the historical text Weilüe, which states:
When Zhuge Liang was ill, he told Wei Yan and the others, "After I die, all of you should be cautious in setting up defences, but do not return here." Wei Yan was then ordered to take Zhuge Liang's command and to not reveal news about Zhuge's death. He did as he was instructed after Zhuge Liang died. When the Shu forces reached Baokou (襃口), Zhuge Liang's death was announced and a funeral was held. Zhuge Liang's Chief Clerk (長史), Yang Yi, was on bad terms with Wei Yan all this while, and he feared that he would be harmed when he heard that Wei Yan had taken charge of the armies. He spread rumours that Wei Yan was planning to surrender to Cao Wei, and then led the soldiers to attack Wei Yan. Wei Yan had no intention of defecting to Cao Wei, so he did not put up resistance and retreated instead, but Yang Yi and his men caught up with him and killed him.
Pei Songzhi, who added the Weilüe account to Wei Yan's biography and annotated the Sanguozhi, commented on the Weilüe account as follows:
I believe this account was derived from hearsay in the enemy state (Cao Wei) and may not be as reliable as compared to the original account (by Chen Shou).
Wei Yan's death was explained in political terms in Injustice to Wei Yan (魏延的千古奇冤), a neoteric article by Zhu Ziyan, a history professor from Shanghai University. In the article, Zhu wrote that Zhuge Liang personally appointed Jiang Wan, Fei Yi and Jiang Wei to be his successors, but Wei Yan's appointments and contributions were greater than those of any of them at the time. Zhuge Liang ostracised Wei Yan and cracked down on him because he wanted to eliminate Wei Yan as a possible obstacle to his appointed successors.
A Wei Yan Shrine (魏延祠) is located in Baique Village, Sanquan Township, Zitong County, Sichuan. In front of the shrine flows a Wei Family River (魏家河). On the plains east of the river, there once stood a Wei Family River Temple (魏家河廟), which had three stone tablets in front of it. One of the stone tablets bore the words "Wei Yan once led soldiers and was stationed here." According to legend, in 231, during the fourth Shu Northern Expedition against Wei, Zhuge Liang ordered Wei Yan to lead a separate force to station south of the Wei Family River. In memory of the incident, the locals built the Wei Family River Temple beside the river and a small bridge called "General Bridge" (將軍橋). The stone tablet was lost when the temple was destroyed in 1968; only the bridge remained, but it is usually submerged until the low tide in June or July. The Wei Yan Shrine was initially demolished by the government but was rebuilt in 1995. A statue of Wei Yan stands in the main hall of the shrine.
Wei Yan appears as a character in the historical novel Romance of the Three Kingdoms by Luo Guanzhong, which romanticises the historical events leading to, and during the Three Kingdoms period.
See the following for some fictitious stories in Romance of the Three Kingdoms involving Wei Yan:Battle of Changsha (fictional)
Zhuge Liang's Northern Expeditions#In fiction
Wei Yan is featured as a playable character in Koei's Dynasty Warriors and Warriors Orochi video game series. He also appears in Koei's Dynasty Tactics 2.