All Wilde had to do was not sue the Marquess of Queensberry (yes the boxing one), a belligerent and vengeful man, for calling him a sodomite, or as he spelled it, a "somdomite." The Marquess was already mad at him for somdomizing his son.
Wilde was the closest thing to being an open homosexual as you could possibly be in a society where it was technically illegal, but almost nobody was prosecuted for it. However, his dumb lawsuit resulted in this thing called discovery, in which it was more or less outright proven that he was, indeed, a true uranian, in the parlance of the time.
Given that it was proven in open court, they really had no choice but to prosecute him criminally too, even though at the time, short of buggering someone on stage, you'd almost never get prosecuted for somdomy.