First and foremost, make sure there is nothing physically wrong with him. Have your pediatrician check him out. If he sails through all of that...when he does BM in his pants, just calmly tell him it is ok and have him clean it up himself. Put a plastic trash can in the bathroom where he drops his soiled clothes, and let him clean himself. Make sure he gets NO attention. Just calmly tell him to deal with it and you keep doing whatever it is you were doing.
I noticed on your profile that it says you are foster parents. Encopresis (pooping in pants) is common in children with emotional issues. It's a sort of control issue, and ownership. Just don't let it be an issue. He poops, he changes and he resumes his play! No yelling, no crankiness, no fussing...