0

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?

  • the problem is that the 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. Jun 15 at 22:06
  • Not sure I thougt it should be before. But I will try again and report. – Blackbam Jun 16 at 17:18
1

WordPress 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');
        }
    }
} );
  • 1
    Thank you very much for this detailed explanation 👍 – Blackbam Jun 17 at 13:52

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.