"Stark Raving Dad" was written specially for Michael Jackson, a fan of the show, who had called Groening one night and offered to do a guest spot. The offer was accepted and a script was written by Al Jean and Mike Reiss, based on an idea pitched by James L. Brooks. Creator Matt Groening and co-executive producer Sam Simon also contributed significantly to the writing. In an early version of the script, Homer decided to take his alcoholic friend Barney Gumble in for rehab, but while there Homer began acting crazily so the doctors assumed he was the one to be committed. It was later changed to Homer being hospitalized for wearing a pink shirt, an idea pitched by Brooks. Jackson pitched several story ideas, such as Bart telling everyone in town that Jackson was coming to his house. He also requested a scene in which he and Bart write a song together and asked that a joke about Prince be changed to one about Elvis Presley.