0

I'm trying to add an action every time someone presses the 'Publish'-button. I've made this function:

function custom_set_category_on_cpt( $post_ID, $post, $update ) {

echo '<p>This does not show, unless I hook it onto admin_head or admin_footer</p>';

if ( 'publish' === $post->post_status ) {

  echo '<p>I can't get anything in here to show... Ever!?</p>';  

}
add_action( 'save_post', 'custom_set_category_on_cpt', 10 );

// Other attempts, without any change of results
//add_action( 'save_post', 'custom_set_category_on_cpt', 10, 3 );
//add_action( 'save_post', 'custom_set_category_on_cpt', 5, 3 );
//add_action( 'save_post', 'custom_set_category_on_cpt', 15, 3 );

I know that the 'save_post'-hook fires, also when it just saves a draft.

I've tried a wide variety of things, but I'm unable to get it to work.

  • One simple thing I do to make sure that the action is properly hooked is to add echo "It's working"; die(); - the last bit will halt any further processing so that the echo'd text doesn't get immediately wiped out. Obviously, that's then removed when I've proved to myself that it's firing. Also handy for checking the content of variables, eg: print_r($update); die(); – Peter HvD Apr 25 '18 at 10:44
1

When editing a post using the admin interface the wp-admin/post.php script redirects you after saving the post. This is done to avoid resubmitting the post request if you refresh the page after submitting. It also means that you're not able to output anything during the save_post action.

case 'editpost':
    check_admin_referer('update-post_' . $post_id);

    $post_id = edit_post();

    // Session cookie flag that the post was saved
    if ( isset( $_COOKIE['wp-saving-post'] ) && $_COOKIE['wp-saving-post'] === $post_id . '-check' ) {
        setcookie( 'wp-saving-post', $post_id . '-saved', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
    }

    redirect_post($post_id); // Send user on their way while we keep working

    exit();

You could do what WordPress does and use cookies to store the message you want to display

<?php

class Your_Class_Name_Here {

  /**
   * Setup
   */
  static function setup() {
    add_action( 'save_post', __CLASS__.'::save_post', 10, 2 );
    add_action( 'in_admin_header', __CLASS__.'::display_message' );
  }

  /**
   * Action: Save post
   * hooked on save_post
   * @param  integer $post_ID
   * @param  WP_Post $post
   */
  static function save_post( $post_ID, $post ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
      return;
    }
    if ( 'publish' !== $post->post_status ) {
      return;
    }
    setcookie( 'Your_Class_Name_Here', 'display_message', time() + DAY_IN_SECONDS, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
  }

  /**
   * Action: Display message
   * hooked on in_admin_header
   */
  static function display_message() {
    if ( ! isset( $_COOKIE['Your_Class_Name_Here'] ) ) {
      return;
    }
    if ( $_COOKIE['Your_Class_Name_Here'] != 'display_message' ) {
      return;
    }
    // Delete the cookie
    setcookie( 'Your_Class_Name_Here', null, -1, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, is_ssl() );
    unset( $_COOKIE['Your_Class_Name_Here'] );
    // Output the message
    echo "Your message here";
  }

}

Your_Class_Name_Here::setup();
0

You also can try below code for specific custom post type only. add_action( 'publish_YOUR_CUSTOM_POST_TYPE_SLUG', 'your_function_name', 999, 3 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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