I have written a simple code to do something after a post is updated or published (its related to the varnish purge). I put my code inside post.php file. Everything was just fine before the latest update (4.8.3), after that all my codes were vanished!! of course it is a normal behavior because the post.php file has been replaced with a new one from update patch. I want to know how can I execute some code after a post is updated or published and my codes do not disappear after a Wordpress update? I don't want to use plugins too :D. Thank you.


3 Answers 3


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.

  • You mean functions.php is not overwritten when the Wordpress is updated? I only have to insert my function and use add action to add my function and then save the file. Is there anything else should I do to my function work?
    – Sinai
    Nov 2, 2017 at 7:09
  • Nothing in the wp-content folder will be overwritten when WordPress is updated, so you can safely add it to wp-content/themes/your-themes/functions.php file. Nov 2, 2017 at 7:15
  • Thank you very much. Final question: if I want to pass arguments to my function I should use add_action( 'save_post', 'your_save_post_function',10,2 ); for a function with two arguments. But how the function is called? for example my arguments are $postID, $status. I mean when the function is called how wordpress knows I need postID as the 1st argument and the $status which is a custom defined variable as the 2nd argument?
    – Sinai
    Nov 2, 2017 at 7:23
  • save_post fires even when a post is unpublished (set back to draft).
    – fuxia
    Nov 2, 2017 at 7:24
  • I updated my answer. You should pass the $post_id to your function. You can check the codex page I linked for a further detailed explanation. Nov 2, 2017 at 7:28
add_action( 'save_post', 'varnish_purge' );
function varnish_purge()
 // code here

You can replace publish_{post} , where {post} is any wordpress post type.

function post_published_notification( $ID, $post ) {
    $author = $post->post_author; /* Post author ID. */
    $name = get_the_author_meta( 'display_name', $author );
    $email = get_the_author_meta( 'user_email', $author );
    $title = $post->post_title;
    $permalink = get_permalink( $ID );
    $edit = get_edit_post_link( $ID, '' );
    $to[] = sprintf( '%s <%s>', $name, $email );
    $subject = sprintf( 'Published: %s', $title );
    $message = sprintf ('Congratulations, %s! Your article “%s” has been published.' . "\n\n", $name, $title );
    $message .= sprintf( 'View: %s', $permalink );
    $headers[] = '';
    wp_mail( $to, $subject, $message, $headers );
add_action( 'publish_post', 'post_published_notification', 10, 2 );

Not the answer you're looking for? Browse other questions tagged or ask your own question.