Create user for statistics only

In the functions.php file in the child theme folder, add this function:


add_action( 'admin_head', function() {
    
    $stats_user_name = 'stats';
    $stats_user_password = 'stats1234';
    $stats_user_email = '[email protected]';

    if(!wp_roles()->is_role( 'shop_stats' )) add_role( 'shop_stats', 'Shop Statistics', get_role('shop_manager')->capabilities );
    if(!empty($stats_user_name) && !username_exists($stats_user_name) ) {
        $stats_user_id = wp_create_user($stats_user_name, $stats_user_password, $stats_user_email);
        $stats_user = get_user_by('id', $stats_user_id);
        $stats_user->remove_role('subscriber');
        $stats_user->add_role('shop_stats');
    }
    if(is_user_logged_in() && current( wp_get_current_user()->roles) == 'shop_stats'){
        $screen = get_current_screen();
        if ($screen->id != 'woocommerce_page_wc-admin') {
            wp_redirect( '/wp-admin/admin.php?page=wc-admin&path=/analytics/overview' );
        }
        remove_menu_page( 'index.php' );                                        //Dashboard
        remove_menu_page( 'edit.php' );                                         //Posts
        remove_menu_page( 'upload.php' );                                       //Media
        remove_menu_page( 'edit.php?post_type=page' );                          //Pages
        remove_menu_page( 'edit.php?post_type=product' );                       //Products
        remove_menu_page( 'wc-admin&path=/marketing' );                         //Marketing
        remove_menu_page( 'edit.php?post_type=elementor_library' );             //Elementor
        remove_menu_page( 'edit-comments.php' );                                //Comments
        remove_menu_page( 'themes.php' );                                       //Appearance
        remove_menu_page( 'plugins.php' );                                      //Plugins
        remove_menu_page( 'users.php' );                                        //Users
        remove_menu_page( 'tools.php' );                                        //Tools
        remove_menu_page( 'options-general.php' );                              //Settings
        remove_menu_page( 'wpcf7' );                                            //Contact Form 7
        remove_menu_page( 'woocommerce' );                                      //Woocommerce
        remove_menu_page( 'wpseo_workouts' );                                   //Yoast

        echo '';

    }
});


IMPORTANTE:

Por razones de seguridad, cambiar las siguientes variables al pegar el código en vuestro archivo:


    $stats_user_name = 'new user';
    $stats_user_password = 'your password';
    $stats_user_email = 'info@your_domain.com';

Upon entering the admin panel, this function will create a new role “Shop Statistics” and a new user with the name and password chosen above.

This new user can be edited from the WordPress user management section. It will also be possible to create new users only for statistics from the WordPress user management itself by choosing “Shop Statistics” in the “User role” option.