Skip to content

Instantly share code, notes, and snippets.

@scheurta
Last active August 29, 2015 14:10
Show Gist options
  • Save scheurta/13ba0cf5e920c8961ac2 to your computer and use it in GitHub Desktop.
Save scheurta/13ba0cf5e920c8961ac2 to your computer and use it in GitHub Desktop.
Prevent deletion of specific user
// MY_USER_ID
define( 'MY_USER_ID', 7 );
/**
* Delete User action, checks for our defined user id
*
*/
function themename_delete_user( $id ){
if ( $id == MY_USER_ID ) {
wp_die( 'You cannot delete this user' );
}
}
add_action( 'delete_user', 'themename_delete_user' );
/**
* Remove MY_USER_ID from user table
*
*/
function themename_pre_user_query( $query ){
global $pagenow, $wpdb;
if ( is_admin() && $pagenow === 'users.php' && get_current_user_id() != MY_USER_ID ){
$query->query_where = $query->query_where . " AND $wpdb->users.ID != " . MY_USER_ID. " ";
}
}
add_filter( 'pre_user_query', 'themename_pre_user_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment