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()
.
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()
.
2 Answers
Reset to default 2So 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']);
}
}
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/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.