I heard that when William Hartnell started getting doddery and forgetting his lines, he refused to retire, implying that he was irreplaceable. So the writers made the character of the Doctor regenerate into a different actor. Was the idea of regeneration planned from the start? Or was it something that was devised to overcome the problem of an aging actor?
The rumor you heard is true - reincarnation in Doctor Who was, in fact, the result of difficulty with (and the failing health of) William Hartnell. According to Wikipedia:
The concept of regeneration was created in 1966 by the writers of Doctor Who as a method of replacing the leading actor. The role of the Doctor had been played by William Hartnell from the programme's inception in 1963 but, by 1966, it was increasingly apparent that Hartnell's health was deteriorating and he was becoming more difficult to work with. Producer John Wiles had, following several clashes with Hartnell, intended to have the actor replaced in The Celestial Toymaker; during two episodes of that serial, the Doctor is invisible (owing to Hartnell being on holiday during the recording). Wiles' plan was for the character to reappear played by a new actor. This proposal was vetoed by Gerald Savory, the BBC's Head of Serials (and Wiles' superior), which led to Wiles leaving before The Celestial Toymaker was produced.3 However, it was apparent that it would not be possible for Hartnell to continue for much longer.
On 29 July 1966, production concluded on the final episode of The Smugglers, the last serial recorded in the third production block.4 During production, Hartnell and producer Innes Lloyd had reached an agreement that he should leave the role, having starred in one more serial that would see a handover to a new actor, which would be the first one produced as part of Season 4. Script editor Gerry Davis proposed that, since the Doctor had already been established as an alien, the character could die and return in a new body. Lloyd took this further by suggesting that the Doctor could do this "renewal" regularly, transforming from an older man to a younger one; this would allow for the convenient recasting of the role when necessary.
This is also briefly discussed in an attendly.com article from 2013. (It is item #17 in their list of "surprising facts" about Doctor Who.)