1

I'm trying to fire an admin notice from within a pre_insert_term filter (have also tried it as an action). Example:

add_filter( 'pre_insert_term', 'pre_insert_term_action', 10, 2 );

function pre_insert_term_action( $term, $taxonomy ) {
    add_action( 'admin_notices', 'show_example_error' );
    return $term;
}

function show_example_error() { ?>
    <div class="error">
        <p>My example warning message.</p>
    </div>
<?php }

So I add a tag in my post and hit the Update button, but it does not show me the admin notice as expected. If I put die() calls within both functions, it is showing me that the functions are firing as expected, first pre_insert_term_action() above, then show_example_error().

When I add my admin_notices action outside of the pre_insert_term_action() function scope, the admin notice does display. So if the functions are firing in the expected order, why is my admin notice not displaying if the action is added in the first firing function?

There's an accepted answer in this related question suggesting to use add_settings_error() but that didn't do anything that I can visibly see.

I also find it weird that if I define a $GLOBALS variable in my pre_insert_term_action() function, it doesn't return a value when I call it in show_example_error().

1

So it looks like there is some page redirecting going on in the post updating process, hence why I can't even access a $GLOBALS variable. The only alternative I found to use was to use a session variable. Something like:

add_filter( 'pre_insert_term', 'pre_insert_term_action', 10, 2 );
add_action( 'admin_notices', 'show_example_error' );

function pre_insert_term_action( $term, $taxonomy ) {
    if ( MY CONDITION THAT WARRANTS A WARNING NOTICE IS TRUE ) {
        if ( session_id() === '' || ! isset($_SESSION) ) {
            session_start();
        }
        $_SESSION['show_my_error_message'] = true;
        return new WP_Error( 'code', 'message' );
    }
    return $term;
}

function show_example_error() {
    if ( session_id() === '' || ! isset($_SESSION) ) {
        session_start();
    }
    if ( isset($_SESSION['show_my_error_message']) ) {
        ?>
        <div class="error">
            <p>My example warning message.</p>
        </div>
        <?php
        unset($_SESSION['show_my_error_message']);
    }
}
0

I'd go for something like this:

function my_notices() {
      $screen = get_current_screen();
      if ($screen->id === 'your-id') { //or anything else
        if (isset($_GET['save'])) { // or $_SESSION or transient

          <div class="error">
            <p>My example warning message.</p>
          </div>

        <?php }
      }
}
add_action('admin_notices', 'my_notices');

Let's see the WP Screen object also. https://codex.wordpress.org/Class_Reference/WP_Screen

EDIT

You can use $_GET, $_SESSION or check for transients set during save_post actions or similar, like the pre_insert_term filter. Good explanation here.

  • The admin notice is dependent upon what happens in the pre_insert_term filter, therefore I need that filter or action to run first to determine whether or not to fire an admin_notices action. – Talk Nerdy To Me Sep 24 at 22:08
  • Yes but this is another approach and adding actions inside filters is not the right way. You should use the right admin_notices action and inside that process your term, getting the post object and eventually display the notice. – huraji Sep 25 at 12:13
  • I can't run the necessary pre_insert_term filter or action from inside the admin_notices action because pre_insert_term has already ran. – Talk Nerdy To Me Sep 25 at 15:55
  • But you can manipulate it. Please formulate your ultimate goal with that. – huraji Sep 25 at 17:05
  • How? My goal is to avoid a user being able to add new/non-existing tags if they're not an administrator and the only way I know how to do that is within the pre_insert_term filter. Then I can't seem to communicate to the admin-notices hook the result of that. – Talk Nerdy To Me Sep 25 at 17:08

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.