Harold has fallen ill and his ogre son-in-law Shrek and daughter Fiona are next in line to be king and queen. Shrek declines, insisting that an ogre as king is a bad idea and that there has to be someone else for the job. With his final few breaths, the king tells Shrek that there is one other heir who can become the new King of Far Far Away: his nephew, Arthur. Shrek sets out to find this new king, along with Donkey and Puss in Boots. As they're sailing off, Fiona runs to the dock and announces to Shrek that he's going to be a father. Shocked, Shrek begins to have nightmares about his future children on the journey to Worcestershire Academy, where they eventually discover that "Artie," who is an academy student, is a loser picked on by the other students - even the nerds. Artie is initially thrilled to be the new king, and excited to be on his way to the throne, until Donkey and Puss inadvertently scare him by talking about things like plague and famine. Panicked, Artie tries to take control of the ship and ends up crashing it on an island where they meet Artie's retired wizard teacher, Merlin.
Meanwhile, Prince Charming has gone to the Poison Apple Bar, where he encounters a slew of fairy tale villains including Captain Hook, the Wicked Queen, Polyphemus, Rumpelstiltskin, Little Red Riding Hood, Mabel the Ugly Stepsister, the Headless Horseman, Stromboli the Puppet Master, and an assortment of black knights, dwarves, trees, and witches. Although they initially despise Charming, he persuades them to join him in a fight for their "happily ever after". The villains feel their side of the story has never been told and now is the time to do it. Charming and the other villains invade the kingdom and pillage for a time before attacking the castle, disrupting Fiona's baby shower. They capture all of Shrek's fairy tale friends (Gingerbread Man, Pinocchio, The Big Bad Wolf (a cross between the Three little pigs and Little Red Riding Hood's wolf), The Three Blind Mice and The Three Little Pigs, Dragon, and Donkey and Dragon's children. Fiona and Lilian try to escape through an underground passage, along with Doris the Ugly Stepsister, Cinderella, Snow White, Sleeping Beauty and Rapunzel; the ladies are captured, however, when Rapunzel betrays them and leads them into a trap. They learn that she is in love with Charming, who plans to make her his queen once he claims the throne.
Captain Hook and some of his pirates track Shrek and company to Merlin's island, where they attempt to capture Shrek and kill the others. Shrek and Artie tag-team them effectively, however, and send the villains running, but not before Hook mentions "King Charming" and the takeover of Far Far Away. Concerned for his wife and their child, Shrek urges Artie to return to the safety of Worcestershire; Artie, however, has other ideas. He cons Merlin into coming out of retirement long enough to use his magic and send them all back to Far Far Away; the spell works, but accidentally causes Puss and Donkey to switch bodies because they were touching each other. They find that Charming is bent on revenge against Shrek for 'stealing' his "happily ever after," and plans to kill Shrek in a play later that night. Charming's men arrive shortly, but another clever ruse by Artie tricks the knights into not taking them into custody. They then break into the castle, where play rehearsal and set design are in full swing, but Charming is not good at rehearsing. In Charming's dressing room, Shrek menaces Charming but Charming is able to summon his men, who burst in and take the four captive.
Charming prepares to decapitate Artie. In an effort to save him, Shrek tells Charming that Artie was just a patsy to take his place as King of Far Far Away. Charming believes Shrek and decides not to kill the boy. Artie, who had just been growing to trust Shrek, is crushed by this and runs away. Donkey and Puss are thrown into the tower with Fiona and the other ladies, where Fiona is growing frustrated with the other princesses and their lack of initiative. Queen Lilian grows fed up when Snow White calls her an old lady, and successfully smashes the stone wall of the prison by head butting the walls. While the women launch a rescue mission for Shrek, who is being held captive elsewhere, Donkey and Puss work to free Gingy, Pinocchio, the wolf and pigs, Dragon, and the Dronkey. As they prepare to enter the castle and join the ladies, they encounter Artie, and Puss and Donkey explain to him that Shrek lied so Charming wouldn't kill him. Artie seems hesitant to believe them.
As the kingdom watches, Charming stages a theatrical performance in which he heroically rides to the rescue of Rapunzel in her (fake) tower and sings, somewhat badly. To Charming's profound annoyance, the chained Shrek wins the audience's support by ridiculing his singing and acting. Just as Charming is about to kill Shrek, Fiona and her friends, along with Puss, Donkey and the Fairy Tale characters, leap onto the stage to confront the villains. It goes awry, however, as the villains largely outnumber the heroes and take them prisoner again. In the nick of time, Artie arrives and convinces the villains to stop and turn over a new leaf, proving himself to possess effective leadership skills. He says a word of wisdom-"Just because somepeople treat you like a loser, it dosen't mean you are one. The thing that matters most is what you think of yourself. If there is something you really want or someone you really want to be then you are standing in your own way." The villains drop their weapons and release their captives. Charming, furious at having been thwarted by this boy, lunges for him with his sword. Shrek blocks the blow and appears to take it in his own chest, leading Charming to exult; the attack missed, however, and the sword is lodged harmlessly under Shrek's arm. Shrek informs Charming that he needs to keep looking for his own happily ever after, "because I'm not giving up mine." Dragon slyly knocks over Rapunzel's tower, which lands on Charming, and the crowd cheers. Charming's crown is sent rolling across the stage by the impact and is caught by Artie. Shrek tells him that the throne is his if he wants it, but it is his decision to make. Artie lifts the crown toward the audience, who cheer him loudly, then sets it on his own head. While the kingdom celebrates their new monarch, Merlin appears and restores Puss and Donkey to their proper bodies, though their tails remain switched.
As Far Far Away is left in the capable hands of young King Arthur, Shrek retires with Fiona to their swamp, having three ogre babies.