4

It's easy enough to find help with admin_notices which:

  • show permanently in the admin screen
  • only show when a particular kind of page is shown
  • show until the user dismisses it

But I can't find help with creating a message which shows until the user leaves that particular page. ie. exactly what WordPress shows routinely with its Post updated. View post. message.

enter image description here

You only see that message up until you decide to move on, you don't see it again, and you don't have to actively dismiss it.

Here's what we've got so far (this creates a permanent message, obviously this is far from the unobtrusive message we're looking for):

function my_admin_notice() {
?>
<div class="updated">
    <p><?php _e( 'Updated!', 'my-text-domain' ); ?></p>
</div>
<?php
}
add_action( 'admin_notices', 'my_admin_notice' );
5
  • What type of admin page are you targeting? e.g. post-new.php or custom plugin page...
    – Christine Cooper
    Apr 9 '15 at 17:06
  • @ChristineCooper when the user publishes a custom post type we're auto generating a separate post (announcement). that's working fine. but i'd like the user to realise and be reminded that he's got auto announce switched on. so the notification would be nice, hence my question. so we're targeting the 'publish_mycpt' hook ... effectively
    – hawbsl
    Apr 9 '15 at 17:21
  • If any of the answer was helpful to you, then consider accepting it. See »What should I do when someone answers my question?« and/or »Why is voting important?«, more information about the WordPress Development model is available at the help center.
    – Nicolai
    Nov 9 '15 at 12:23
  • No worries, I'm just taking care of old business from time to time and today was the day. If and when you'll do it is at the end completely up to you.
    – Nicolai
    Nov 9 '15 at 16:48
  • @hawbsl If your question has been answered, could you please accept the solution. Thanks. Jan 4 '17 at 20:19
7
+25

The short answer is: Use Query Strings.

If you notice in the address bar immediately after you publish a Post... You will see something similar to this: domain.com/wp-admin/post.php?post=4935&action=edit&message=6

There's a few different Query Variables:

  1. post contains the ID of the Post being edited.
  2. action is saying we're currently "editing" the Post.
  3. message refers to what Admin Notice should be displayed depending on what was done to the Post.

Look here for an example on seeing the default Post Type messages and how to customize the messages (a/k/a Admin notices) specifically for your Custom Post Type.

The above example is relative to a Custom Post Type and WordPress handles all the conditionals for you. All you have to do is customize the messages.

If you wanted to do this on a custom settings page for your plugin for example. You would need to take several steps to accomplish the same thing.

In a nut shell:

Send the user to your custom settings page WITH an extra Query Variable appended to the URL.

Hook into admin_notices.

Inside the callback function you specify for the admin_notices hook, have an array of messages, where they array key will match the value of your Query Variable and the array value will be the appropriate message.

Check to see if your Query Variable is present. (Be sure to check that the user is specifically on your plugin's setting page using: get_current_screen(); - otherwise you'll be checking globally across every admin page.)

Perform additional conditional checks for the different possible values of your Query Variable and display the appropriate Admin Notice depending on the value of your Query Variable.

So basically:

  • Check if the user is on your plugin's settings page.
  • Check if your Query Variable is present.
  • Check if the value of your Query Variable matches that of something expected.
  • Select appropriate message depending on the expected value matched from your Query Variable with an array key in your messages array.
  • Display Admin Notice with message matched from messages array key.
2

This answer assumes you want to do this on a post edit screen.

Explanation:

Basically, you need some conditions:

  1. To address a specific admin screen:

  2. To only show the message until the specific page has been left:

Additionally - of course - usage of the admin_notices hook. And - for below example - the save_post hook.

Example:

function wpse183752_the_admin_notice() {
    // get screen information
    $screen = get_current_screen();
    // screen condition    
    // TODO: change to the screen you want to address
    if ( $screen->id != 'post' ) {
        return;
    }
    // query variable condition
    if ( ! isset( $_GET[ 'the_message' ] ) || $_GET[ 'the_message' ] != 'show' ) {
        return;
    }
    ?>
    <div class="updated">
        <p>
            <?php _e( 'The Message', 'the-text-domain' ); ?>
        </p>
    </div>
    <?php
}
add_action( 'admin_notices', 'wpse183752_the_admin_notice' );

function wpse183752_the_message( $old_query_string ) {
    // add the new query variable
    $new_query_string = add_query_arg( 'the_message', 'show', $old_query_string );
    // return the new query string
    return $new_query_string;
}

function wpse183752_set_message_variable() {
    // usage of wpse183752_the_message() callback with redirect_post_location
    // to setup the query variable on a hook, here save_post
    add_filter( 'redirect_post_location', 'wpse183752_the_message' );
}
// TODO: change to the hook you need
add_action( 'save_post', 'wpse183752_set_message_variable' );

Note:

Above example is pretty much a generic one. It addresses the post edit screen of the built-in post type post. You have to change some things to make it work for you, see the ToDo's in above example. As you said yourself you probably want to address publish_mycpt, which is a {status}_{post_type} type of hook, see Codex: Post Status Transitions for more information.

0

Not entirely sure I understand what you are doing but would it be feasible to add an option (add_option) during the CPT post publish and then check for the existance of that option and if there, display the notice. Within the function you have to display the notice, you could also remove the option to stop it showing again.

Alternatively add user meta and remove it when the notice is displayed - best option if you are only targeting a single user

1
  • thanks @Mike that sounds like a possible. i wondered if there was a more built in way since wp itself obviously has the capability to show the Post published notice only briefly
    – hawbsl
    Apr 21 '15 at 16:45

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.