Thing is, the code can go to many places. For me, personally, is to develop own plugin and add code there.
If you need to something execute at certain point, use add_action
hook.
From what you wrote, this question might be helpful: add_action hook for completely new post?