My feeling is that he is trying to get your attention. There are many things that might cause a child to seek attention in this way. If a parent is working all the time and never home, in addition to being a single parent, and the kid is a latchkey kid, this might happen.
If a child feels alone and lonely, then he could look for ways to get his parent’s attention. If he can’t get attention with positive ways, he might try negative ways of getting attention. So the peeing. It drives a parent crazy. Most parents will feel shame that their kid pees himself at school. So they might spank him or try to shame him.
But that plays right into his goal. He doesn’t care whether the attention is negative or positive, so long as his parent is paying attention. Spanking and shaming, in this case, could be what he wants in some form. Of course, he doesn’t really want to be shamed, but he may want parental attention or any attention more than he wants anything else.
If you see a family therapist, you may be able to get some insight into your son’s psychology. You may learn different ways of parenting that will help with this problem. But this is a serious thing and it is good that you are asking for help.
I hope you will stop spanking and stop thinking about ways to shame him into behaving well. They clearly don’t work. They shouldn’t work. They don’t work with healthy kids, except to make them feel bad about themselves and potentially give them problems later on in life. In any case, there are other ways to discipline children that don’t involve corporal punishment, so why not use the less aggressive and more effective way?
How do you discipline kids without aggression? Well, partly you need training. Partly it is philosophy. Partly it is knowledge about how kids work. The easiest way to get this knowledge is through a therapist, but you can research it yourself. There’s a lot involved, but it is worth learning.