public
Last active

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
<?php
 
// Can be called whenever.
function add_admin_notice( $code, $message, $type = 'error' ) {
global $wp_admin_notices;
 
if ( ! isset( $wp_admin_notices ) )
$wp_admin_notices = array();
 
$wp_admin_notices[] = array(
'code' => $code,
'message' => $message,
'type' => $type,
);
}
 
// Gets called once, after the 'all_admin_notices' hook.
function do_admin_notices() {
if ( defined( 'DOING_AJAX') && DOING_AJAX )
return;
 
global $wp_admin_notices;
 
if ( ! isset( $wp_admin_notices ) )
return;
 
foreach ( $wp_admin_notices as $notice ) {
printf( '<div id="setting-error-%1$s" class="%2$s settings-error"><p>%3$s</p></div>',
$notice['code'],
$notice['type'],
$notice['message']
);
}
 
unset( $GLOBALS['wp_admin_notices'] );
}
 
// Save all notices on shutdown.
function save_admin_notices() {
global $wp_admin_notices;
 
if ( isset( $wp_admin_notices ) )
set_transient( 'admin-notices-' . get_current_user_id(), $wp_admin_notices, 30 );
}
add_action( 'shutdown', 'save_admin_notices' );
 
// Load all notices on init
function load_admin_notices() {
if ( false !== $notices = get_transient( 'admin-notices-' . get_current_user_id() ) ) {
global $wp_admin_notices;
 
if ( ! isset( $wp_admin_notices ) )
$wp_admin_notices = array();
 
$wp_admin_notices = array_merge( $wp_admin_notices, $notices );
delete_transient( 'admin-notices-' . get_current_user_id() );
}
 
}
add_filter( 'admin_init', 'load_admin_notices' );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.