It is not known when Wei Wen was born. His father was Wei Xuanyan , who served as a prefectural prefect late in the reign of . Wei Wen's -- the daughter of Wei Xuanyan's younger brother Wei Xuanzhen -- was the wife and crown princess of Emperor Gaozong's son the Crown Prince, and on that account was quickly promoted. After Emperor Gaozong's death in 683, Li Zhe took the throne , but soon ran afoul of his mother , who retained power as regent, partly over his desire to promote Wei Xuanzhen to be . In spring 684, she deposed Emperor Zhongzong to be the Prince of Luling and exiled him and his wife. Wei Wen thus, for the time being, received no help from his relationship with his cousin, and when he served as a probationary official, he was accused of receiving bribes and removed from his office.
Meanwhile, though, under a plan drafted by Emperor Zhongzong's sister Princess Taiping and concubine , Emperor Zhongzong's son by another concubine, would be named emperor. Empress Wei would serve as empress dowager and regent, while Emperor Zhongzong's younger brother the Prince of Xiang, himself a former emperor, would serve as coregent; Consort Shangguan subsequently formalized this in a will she drafted posthumously for Emperor Zhongzong. Wei Wen and another chancellor, Ji Chuna, however, opposed this plan on the grounds that this would require her and Li Dan to frequently confer, which violated Confucian principles that a brother-in-law and a sister-in-law should not converse with each other. Under Ji's and Wei Wen's insistence, the other chancellors did not dare to oppose their will, and Li Dan was not made coregent. Li Chongmao soon took the throne , with Empress Wei serving as empress dowager and regent.