Does WordPress have a built in message function for presenting notifications to users?

Here’s an idea: use the save_post hook to set a session containing the message you want to show the user and then redirect to the home page. In the home page template, check for the presence of that session and show the message to the user.

Something like this:

functions.php:

add_action( 'save_post', 'wpse60249_save_post' );

function wpse60249_save_post( $post_id ) {
    session_start();
    $_SESSION[ 'message' ] = __( 'Your text here...' );
}

index.php (or appropriate template):

if ( isset( $_SESSION[ 'message' ] ) :
    echo $_SESSION[ 'message' ];
    unset( $_SESSION[ 'message' ] );
endif;

Edit 10/21/2012

When I wrote this answer, I did not know that WordPress resets the $_SESSION variable but I learned that a couple of weeks ago while working on a project for a client. Thanks for the reminder about this question, @kaiser! Here is updated code using transients instead:

functions.php:

add_action( 'save_post', 'wpse60249_save_post' );

function wpse60249_save_post( $post_id ) {
    session_start();
    set_transient( 'temporary_message', __( 'Your text here...' ), 60*60*12 );
}

index.php (or appropriate template):

if ( false !== ( $temp_message = get_transient( 'temporary_message' ) ) :
    echo $temp_message;
    delete_transient( 'temporary_message' );
endif;

Leave a Comment