WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to echo infomation to admin/editor screen after publishing a post.? (6 posts)

  1. MarkRH
    Member
    Posted 1 year ago #

    I've added a function to my theme's functions.php file that executes when a post is published:

    add_action('publish_post', 'mrh_my_function', 99);

    Anyway, the function itself works fine; however, I want to echo out some information to the post edit screen after it's finished the publishing process to display status information that my function was able to do xyz or it wasn't. How do I go about this without getting a headers already sent error if I do a straight echo within my function?

    Suppose I can just log what I need to a text file that I can view but there should be another way.

    Thanks,
    Mark H.

  2. bcworkz
    Member
    Posted 1 year ago #

    You should be able to expand the message facility used by that screen to display things like "Post published" etc. I haven't done this, but it appears you hook the 'post_updated_messages' filter to add your message to the array passed. The messages are keyed by post or page and by number.

    To display the message, you call the same url that was used to open the screen, with the url parameter &message=n added where n is the number key for your message you added to the array.

    I'm not sure if this really helps you, as a message is already displayed when the post is published. Not sure if you can display multiple messages. Maybe you can hack the default message, then unhack it a bit later?

    Not so much an answer here as a possible idea to pursue.

  3. MarkRH
    Member
    Posted 1 year ago #

    Well, if I put the following in my functions.php (I found this code in another post here):

    function mrh_updated_messages( $messages ) {
       global $post, $post_ID;
       $messages["post"][6] = 'Post Published and Tweeted.';
       return $messages;
    }
    
    add_filter('post_updated_messages', 'mrh_updated_messages');

    It will change the default message; however, I need to change that message within my mrh_my_function() that I mentioned above during the publish process. If I move the add_filter there, then the default message is not modified. Hmmm..

  4. bcworkz
    Member
    Posted 1 year ago #

    Maybe try a global variable that is normally empty that is added to the default message by your messages filter function. Your other function then can alter the message by adding text to the global variable?

    Not sure how to set it back to empty once you're done using it. Once again more of an idea than a solution.

  5. MarkRH
    Member
    Posted 1 year ago #

    I got it working by using session variables. At the top of my functions.php I added:

    session_start();
    date_default_timezone_set('America/Chicago');
    define("LOG_DATE_FORMAT","[d-M-Y h:i:s A T O] - ");  // [10-Oct-2012 05:46:34 AM CDT -0500] -
    if (empty($_SESSION['date-started'])) {
       $_SESSION['date-started'] = date(LOG_DATE_FORMAT);
    }
    $_SESSION['date-now'] = date(LOG_DATE_FORMAT);

    I added the dates so I can check the times things happen with another script that just echos out my session variables.

    Within mrh_my_function that happens during the publish process I added something like:

    $_SESSION['admin_msg'] = '<p>Post Tweeted! '.date(LOG_DATE_FORMAT).'</p>';
    $_SESSION['mrh_my_function'] += 1; // keep track of times entered function

    and removed the add_filter reference.

    Then in my functions.php I modified the other function to have:

    function mrh_updated_messages( $messages ) {
       global $post, $post_ID, $mrh_message;
       $messages["post"][6] = sprintf( __('Post published. <a href="%s">View post</a>'), esc_url( get_permalink($post_ID) ) ).$_SESSION['admin_msg'];
       return $messages;
    }
    
    add_filter('post_updated_messages', 'mrh_updated_messages');

    Now, when I publish a post, I can see the updated information via the admin_msg session variable.

  6. MarkRH
    Member
    Posted 1 year ago #

    Just wanted to says thanks for your help. Going to mark this thread as resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic