how to execute some code after a post is published in WordPress [duplicate]

You’re looking for the save_post action. This allows you to add a function when a post is saved (updated).

You can hook into it like this:

function your_save_post_function( $post_id ) {
}

add_action( 'save_post', 'your_save_post_function' );

Remember to not change WordPress Core files, as these will be overwritten when WordPress is updated. You can put this code in your functions.php file, or anywhere else in your theme folder.