Stokes declared at 415 for nine, with South Africa trailing by 241 runs at the close after surviving a tricky nine over spell before stumps.
The tourists, who lead the three-match series 1-0, reached 23 without loss in their second innings with Sarel Erwee on 12 and Dean Elgar on 11 but they now face a real battle against England’s attack on Saturday.
The England captain and wicketkeeper-batsman Foakes put on a 173-run partnership for the sixth wicket despite all the hoopla of the ultra-aggressive “Bazball” style established under Stokes’ captaincy and the guidance of coach Brendon McCullum.
It was a much-needed contribution after South African fast Anrich Nortje, who had a fantastic opening effort in the morning session, struck twice to dismiss England’s overnight batters.
With Nortje adding reverse swing to his top pace and the Yorkshireman edging to Erwee at first slip, Jonny Bairstow fell one run short of reaching his half century.
Zak Crawley, England’s opening batter, made a solid 38 runs before another excellent delivery from Nortje caused an edge to wicketkeeper Kyle Verreynne.
Thus, England’s score at the time was 147-5, which was four runs short of South Africa’s first-inning total. However, before lunch, Stokes and Foakes got to work to start extending the lead.
Stokes demonstrated his purpose by smashing spinner Simon Harmer over mid-wicket for six, while Keshav Maharaj’s lbw call against Foakes was first upheld but was later reversed on appeal.
After lunch, Stokes smashed Harmer over mid-on and into the balcony of the England dressing room to reach his half-century, but he largely restrained his aggressiveness throughout his innings.
Due to the weaker ball on the challenging Old Trafford surface, South Africa’s seam attack struggled to pick up any wickets during the whole afternoon session.
He looked well delighted as he went off undefeated because it had been a good innings in which he had at first played the necessary supporting role to his skipper before taking the lead in building up England’s score.
Foakes was thrilled to get his second century against a South Africa assault that had utterly destroyed England in the first test at Lords. Foakes’ maiden century came on his debut against Sri Lanka in Galle in November 2018.