WordPress将您重定向回user-edit.php
在成功更新用户后返回页面,因此admin_notices
在你的sulock_save_profile_fields()
, 由于重定向,消息(您的自定义管理通知)永远不会显示。
解决这个问题的一种方法是通过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 );
} );
}
然后钩住
load-user-edit.php
当
user-edit.php
页面已加载,并从此处添加管理员通知:(the
updated
以下项目由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 );
}
还有钩子:
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\');
}
}
} );