Outils pour utilisateurs

Outils du site


informatique:wordpress:cacher_le_premier_administrateur

Cacher Le Premier Administrateur

Date de création : 2022/08/02 11:10

 
 
add_action('pre_user_query', 'pre_user_query');
/**
 * Remove user from all user queries.
 * @global \wpdb $wpdb WordPress database abstraction object.
 *
 * @param \WP_User_Query $user_search The current WP_User_Query instance,
 *                                    passed by reference.
 */
function pre_user_query($user_search)
{
 
    /**
     * @var \WP_User $current_user \WP_User object for the current user.
     */
    $current_user = wp_get_current_user();
    $user = wp_get_current_user();
    if ($user->ID != 1) { // Is not administrator, remove administrator (you can add any user-ID)
        global $wpdb;
        $user_search->query_where = str_replace(
            'WHERE 1=1',
            "WHERE 1=1 AND {$wpdb->users} . ID<>1",
            $user_search->query_where
        );
    }
}
 
add_filter('views_users', 'wpse230495_modify_user_views');
 
function wpse230495_modify_user_views($views)
{
 
    if (get_current_user_id() === 1) {
        return $views;
    } // bow out if we're user number 1
 
    // filter the 'all' count and remove 1 from it
    $views['all'] = preg_replace_callback(
        '/\(([0-9]+)\)/',
        function ($matches) {
            return '(' . ($matches[1] - 1) . ')';
        },
        $views['all']
    );
 
    // filter the 'administrator' count and remove 1 from it
    $views['administrator'] = preg_replace_callback(
        '/\(([0-9]+)\)/',
        function ($matches) {
            return '(' . ($matches[1] - 1) . ')';
        },
        $views['administrator']
    );
 
    // alternatively, uncomment next line if you want to remove Administrator view completely
    //   unset( $views["administrator"] );
 
    return $views;
}
 
add_action('template_redirect', 'wpse230495_author_archive_redirect');
 
function wpse230495_author_archive_redirect()
{
    if (is_author() && get_the_author_meta('ID') === 1) {
        wp_redirect(home_url(), 301);
        exit;
    }
}

Page dans la catégorie:

informatique/wordpress/cacher_le_premier_administrateur.txt · Dernière modification : 2023/08/13 15:07 de john

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki