Writing a little plugin my code is like this:
add_action( 'edit_user_profile_update', 'sulock_save_profile_fields' );
function sulock_save_profile_fields( $user_being_edited_id ) {
if(update_user_meta( $user_being_edited_id, 'sulock_permanently_locked', $permlock)) {
update_user_meta($user_being_edited_id, 'sulock_permlock_meta',new Sulock\LockMeta());
if($permlock) {
sulock_admin_notice(__('This user has been permanently locked by you.',SULOCK_TEXTDOMAIN),'notice notice-warning');
} else {
sulock_admin_notice(__('This user has been permanently locked by you.',SULOCK_TEXTDOMAIN),'notice notice-warning');
}
}
}
// a simplified function for admin notices
function sulock_admin_notice($message,$class) {
add_action('admin_notices',function() use ($message,$class) {
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
});
}
The sulock_admin_notice()
function is working if called from the code directly, but if I call it from the hook the notice is never shown. Nethertheless I assume that the hook is executed before the UI is loaded and therefore the admin notices should be registered.
Whats going wrong and how can I fix it?
Writing a little plugin my code is like this:
add_action( 'edit_user_profile_update', 'sulock_save_profile_fields' );
function sulock_save_profile_fields( $user_being_edited_id ) {
if(update_user_meta( $user_being_edited_id, 'sulock_permanently_locked', $permlock)) {
update_user_meta($user_being_edited_id, 'sulock_permlock_meta',new Sulock\LockMeta());
if($permlock) {
sulock_admin_notice(__('This user has been permanently locked by you.',SULOCK_TEXTDOMAIN),'notice notice-warning');
} else {
sulock_admin_notice(__('This user has been permanently locked by you.',SULOCK_TEXTDOMAIN),'notice notice-warning');
}
}
}
// a simplified function for admin notices
function sulock_admin_notice($message,$class) {
add_action('admin_notices',function() use ($message,$class) {
printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
});
}
The sulock_admin_notice()
function is working if called from the code directly, but if I call it from the hook the notice is never shown. Nethertheless I assume that the hook is executed before the UI is loaded and therefore the admin notices should be registered.
Whats going wrong and how can I fix it?
Share Improve this question asked Jun 12, 2019 at 13:06 BlackbamBlackbam 57511 silver badges28 bronze badges 2 |1 Answer
Reset to default 1WordPress redirects you back to the user-edit.php
page upon successful user update, so while the admin_notices
has yet been fired in your sulock_save_profile_fields()
, the message (your custom admin notice) is never displayed because of the redirection.
And one way to fix it, is by filtering the redirect URL via the wp_redirect
filter:
// In sulock_save_profile_fields()
if ( update_user_meta( $user_being_edited_id, 'sulock_permanently_locked', $permlock ) ) {
update_user_meta( $user_being_edited_id, 'sulock_permlock_meta', new Sulock\LockMeta() );
add_filter( 'wp_redirect', function( $location ) use ( $permlock ) {
return add_query_arg( 'permlock', $permlock, $location );
} );
}
Then hook to load-user-edit.php
which is fired when the user-edit.php
page is loaded, and add the admin notice from there: (the updated
item below is set by WordPress)
add_action( 'load-user-edit.php', function(){
if ( ! empty( $_GET['updated'] ) && isset( $_GET['permlock'] ) ) {
if ( $_GET['permlock'] ) {
sulock_admin_notice(__('Message here.', SULOCK_TEXTDOMAIN), 'notice notice-warning');
} else {
sulock_admin_notice(__('Message here.', SULOCK_TEXTDOMAIN), 'notice notice-warning');
}
}
} );
Alternatively, you could (or might want to) use the transients API:
// In sulock_save_profile_fields()
if ( update_user_meta( $user_being_edited_id, 'sulock_permanently_locked', $permlock ) ) {
update_user_meta( $user_being_edited_id, 'sulock_permlock_meta', new Sulock\LockMeta() );
set_transient( 'su_updated', [ 'permlock' => $permlock ], 30 );
}
And the hook:
add_action( 'load-user-edit.php', function(){
if ( ! empty( $_GET['updated'] ) ) {
$data = get_transient( 'su_updated' );
if ( $data && $data['permlock'] ) {
sulock_admin_notice(__('Message here.', SULOCK_TEXTDOMAIN), 'notice notice-warning');
} elseif ( $data ) { // the transient exists (not expired)
sulock_admin_notice(__('Message here.', SULOCK_TEXTDOMAIN), 'notice notice-warning');
}
}
} );
admin_notices
hook is executed before your profile edit hook is fired so you're adding actions to a hook that will not be fired again. – Paul G. Commented Jun 15, 2019 at 22:06