9

Does Wordpress have something similar to Drupal's drupal_set_message function? I want to notify the user of something and was hoping there was a built in API call to do this.

2
  • Where do you want to notify the user? WordPress has nothing like this built-in, so you'll have to either find a plug-in or roll your own using something like Toastr (if you want pop-ups). Jul 31, 2012 at 2:33
  • A client wants us to redirect to the homepage when a contributor submits a post for review instead of back to the edit post screen. I want to let the user know that their post has been submitted. Jul 31, 2012 at 2:42

2 Answers 2

4

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;
5
  • That was what I was thinking...but WP doesn't support sessions so well :s I'll see what I can come up with. Thanks ;) Jul 31, 2012 at 2:53
  • Check out this article. Jul 31, 2012 at 3:02
  • 1
    $_SESSION doesn't work properly in WP. They get unset by core during start up/request start.
    – kaiser
    Oct 21, 2012 at 8:01
  • Are transients somehow tied to the particular user viewing the page like sessions? Wouldn't the edited code using transients show the message for all users. Although with the delete the window of time is pretty short, it still seems possible for another user to get the message instead or also.
    – User
    Mar 14, 2017 at 9:32
  • you are missing a braked in the else
    – helle
    Apr 1, 2020 at 13:53
2

There's an update and an error message, that you can use on the admin_notices hook.

Test Plugin

/* Plugin Name: Admin Notice w test message */
function wpse60244_custom_admin_notice()
{
    return print '<div id="message" class="updated fade"><p><strong>Hi!</strong> Look at me! In theory I should even fade out after a short time span!</p></div>';
}
add_action( 'admin_notices', 'wpse60244_custom_admin_notice' );

You can throw this in your plugins folder and give it a test.

Sidenotes

You could - in theory also use the admin_head hook to trigger the adding of the action. If you only need it on specific admin UI screens/pages, then check for global $taxnow, $pagenow; or use the admin_head-{$hook_suffix} hook.

3
  • Thanks Kaiser; I had forgotten about that. I assumed he wanted the message to show on the front end, though. Btw: return print?! Hehe ;-) Jul 31, 2012 at 12:12
  • 1
    @Joseph Little tired today. What about return print?
    – kaiser
    Jul 31, 2012 at 12:32
  • I guess that is valid - I thought that print didn't return anything but according to the docs, it always returns 1. Jul 31, 2012 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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