Sophocles was born about 496 BCE in Colonus Hippius, which was outside the walls of Athens at the time Sophocles was born, and is now part of the city of Athens. Sophocles paid tribute to Colonus in one of his last plays, Oedipus at Colonus, written shortly before his death in about 406 BCE.
Sophocles lived during almost the entirety of what is called the "Golden Age" of Athens, which is the period from the time Athens first came into conflict with Persia in about 500 BCE, to its defeat by Sparta, which ended the Peloponnesian Wars in 404 BCE. It was during the "Golden Age" that Athens was at the height of its political power and cultural influence.
It's not known exactly when Sophocles started writing plays, but he came to prominence as a playwright in 468 BCE, when he won first prize in the Festival of Dionysus drama competition in Athens, defeating Aeschylus (c. 525–c. 455 BCE), who is considered the "father of tragedy."
Scholars believe that Sophocles wrote over 120 plays, of which only seven complete plays exist. It's conceivable that one or more of the plays lost to history might have been even better than the ones that have come down to us, although Aristotle considered Oedipus Rex a near-perfect tragedy.
At the time Sophocles wrote Oedipus Rex in about 429 BCE, Athens and Sparta had been involved in the Peloponnesian War for the first two years of what would be a twenty-seven year conflict.
The war seems to have had little detrimental effect on the cultural environment in Athens. The drama competitions at the Festival of Dionysus continued to be held during the war, and some of the greatest ancient Greek plays were written during that time by tragic playwrights Sophocles, Euripides (c. 480–c. 406 BCE), and Aristophanes (c. 446–c. 386 BCE), considered the greatest ancient Greek comic playwright.
The plague afflicting the people of Thebes at the beginning of Oedipus Rex is believed to have been influenced by the plague that afflicted the people of Athens in 430 BCE, a year before Oedipus Rex was written and performed at the Festival of Dionysus. References to the plague also appear in other plays written at that time, including Euripides’s Hippolytus, written in 428 BCE.
Sophocles wrote over 120 plays during his long lifetime. He likely participated in up to 30 drama competitions at the Festival of Dionysus, which he is believed to have won as many as 24 times. Sophocles was a busy playwright.
At the same time, Sophocles was also actively involved with his civic and military duties with the city of Athens. It's seems likely that Sophocles was influenced in his playwriting by events happening in the world around him, such as the war and the plague, and that his plays would naturally reflect the social, cultural, and political environment in which he wrote them.
It's unlikely, though, that Sophocles wrote his plays for any purpose other than that for which they were intended: moral instruction. Sophocles's plays were based on myths, legends, and religious beliefs, not prominent contemporary personages or current events.
Of all the well-known playwrights of his time, Sophocles is considered to have adhered most closely to Greek moral and religious orthodoxy, including the predominant role of fate in people's lives, the will of the gods, and the duty of the individual to support and protect the state. Sophocles's plays move inexorably from problem to resolution, and invariably impart a moral lesson along the way.