Before there was pig Latin, there was dog Latin and even hog Latin, which is probably how we got to pig Latin. But dog Latin and hog Latin, aren't anything like pig Latin, other than their names being similar. Those first two refer to a kind of fake, made-up Latin.
In "Love's Labor Lost," none other than Shakespeare indulges in a bit of dog Latin:
Costard: Go to; thou hast it ad dunghill, at the fingers' ends, as they say.
Holofernes: O, I smell false Latine; dunghill for unguem.
After Costard says "ad dunghill," Holofernes explains that Costard is using false Latin. Costard has used "ad dunghill" because it sounds enough like "ad unguem," a Latin phrase for something being done to an exact measure or standard. "Unguem" means "fingernail," and the phrase comes from checking the smoothness of marble. The joke is much funnier when you explain it at length.
Then in an 1844 issue of United States Magazine and Democratic Review, a monthly political journal published in the 19th century, Edgar Allan Poe mentioned both dog Latin and pig Greek, and not in a kind way. But also not referring to the pig Latin we know.
Pig Latin was likely invented in the late 1800s by kids who wanted to talk without adults understanding them. But as we've seen, it's not exactly the Enigma code. The Oxford English Dictionary has the earliest written use of the phrase pig Latin in 1896, when a J. Willard wrote in "The Atlantic":
They all spoke a queer jargon which they themselves had invented. It was something like the well-known 'pig Latin' that all sorts of children like to play with.
By the early 20th century, everybody who was anybody knew about pig Latin. Enough people that Arthur Fields could release the record "Pig Latin Love" in 1919. The recording is hardly hi-fi, but it does have a clear example of pig Latin. Then, in case you're still hopelessly uncool, he provides a translation in the second chorus.
In the 1930s, the Three Stooges used pig Latin in their short films. Moe and Larry even provided a primer for Curly in 1938's "Tassles in the Air." Curly doesn't learn very quickly, but "ixnay" and "amscray" became part of people's vocabulary thanks to the trio's work in expanding the language.