Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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). –  Joseph Jul 31 '12 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. –  SomethingOn Jul 31 '12 at 2:42
    
Please add this as edit to your question. Thanks. –  kaiser Jul 31 '12 at 12:31
add comment

2 Answers

up vote 2 down vote accepted

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;
share|improve this answer
    
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 ;) –  SomethingOn Jul 31 '12 at 2:53
    
Check out this article. –  Joseph Jul 31 '12 at 3:02
1  
$_SESSION doesn't work properly in WP. They get unset by core during start up/request start. –  kaiser Oct 21 '12 at 8:01
add comment

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.

share|improve this answer
    
Thanks Kaiser; I had forgotten about that. I assumed he wanted the message to show on the front end, though. Btw: return print?! Hehe ;-) –  Joseph Jul 31 '12 at 12:12
1  
@Joseph Little tired today. What about return print? –  kaiser Jul 31 '12 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. –  Joseph Jul 31 '12 at 12:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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