I want to display an admin_notice error message on the custom admin page of my plugin - but I want it to only be displayed from a jQuery event.
All of my jQuery is working. I can send data, see alerts, receive data. But I cannot get the error message to work from the jQuery event.
This displays the error message on every screen in the dashboard.
add_action('admin_notices', 'my_error_notice');
function my_error_notice () {
echo '<div class="error"><p>This is my error message.</p></div>';
}
This displays the error message on my admin page only.
add_action('admin_notices', 'my_error_notice');
function my_error_notice () {
global $my_admin_page;
$screen = get_current_screen();
if ( $screen->id == $my_admin_page ) {
echo '<div class="error"><p>This is my error message.</p></div>';
} else {
return;
}
}
I want to only display the error message from a jQuery event.
add_action('wp_ajax_test_function', 'test_function');
function test_function() {
add_action('admin_notices', 'my_error_notice');
}
function my_error_notice () {
global $my_admin_page;
$screen = get_current_screen();
if ( $screen->id == $my_admin_page ) {
echo '<div class="error"><p>This is my error message.</p></div>';
} else {
return;
}
}
jQuery
jQuery(document).ready(function() {
jQuery("#my-button").click(function() {
jQuery.ajax({
type: 'POST',
url: myAjax.ajaxurl,
data: {
"action": "test_function"
}
});
});