0
\$\begingroup\$

I want to restrict a specific user to use only one plugin. I don't want him to see any other pages on the WordPress admin dashboard like the Plugins page, Posts, or other pages. I want to show him just one link in the admin dashboard and let him to use just that page.

I want to do it programmatically and don't want to use a plugin for it.

I implemented the same functionality to restrict users by the code below but I want to know is there any better solution for it or not. It redirects the user to '/wp-admin/edit.php?post_type=product' if he doesn't visit the valid pages that I declared in the $pages array. I'm using WooCommerce, the post_type=product is for that.

Could you help me to achieve this, please? Thank you.

The code I wrote in functions.php file before to achieve this, the issue if I have to set all pages and plugins ID manually:

add_action('current_screen', 'checkUserPermissionsInThisScreen');

function checkUserPermissionsInThisScreen(){

    if (is_admin()){

      $a = get_current_screen();
      $currentUserName = wp_get_current_user()->user_login;

      if ($currentUserName === "testuser") {

         $pages = array(
          'dashboard',
          'toplevel_page_wpcf7',
          'contact_page_wpcf7-new',
          'contact_page_wpcf7-integration',
          'edit-shop_order',
          'edit-shop_coupon',
          'woocommerce_page_dgwt_wcas_settings',
          'woocommerce_page_wc-reports',
          'woocommerce_page_wc-settings',
          'woocommerce_page_wc-status',
          'woocommerce_page_wc-addons', 
          'woocommerce_page_alg-wc-renumerate-orders-tools',
          'themes',
          'customize',
          'users',
          'tools',
          'import',
          'export',
          'site-health',
          'export-personal-data',
          'erase-personal-data',
          'tools_page_action-scheduler',
          'options-general',
          'options-writing',
          'options-reading',
          'options-discussion',
          'options-media',
          'options-permalink',
          'options-privacy',
          'toplevel_page_agilewc',
          'toplevel_page_smush',
          'shop_order'
        );


        remove_menu_page( 'themes.php' ); 
        remove_menu_page( 'woocommerce' );
        remove_menu_page( 'wpcf7' );
        remove_menu_page( 'index.php' );
        remove_menu_page( 'edit-comments.php' );
        remove_menu_page( 'users.php' );
        remove_menu_page( 'tools.php' );
        remove_menu_page( 'options-general.php' );
        remove_menu_page( 'smush' );

        //Remove the "WooCommerce Recent Reviews" widget.
        remove_meta_box('woocommerce_dashboard_recent_reviews', 'dashboard', 'normal');
        //Remove the "WooCommerce Status" widget.
        remove_meta_box('woocommerce_dashboard_status', 'dashboard', 'normal');
        //Hide the "Coupon data" meta box.
        remove_meta_box('woocommerce-coupon-data', $a->id, 'normal');
        //Hide the "Order data" meta box.
        remove_meta_box('woocommerce-order-data', $a->id, 'normal');
        //Hide the "Items" meta box.
        remove_meta_box('woocommerce-order-items', $a->id, 'normal');
        //Hide the "Downloadable product permissions" meta box.
        remove_meta_box('woocommerce-order-downloads', $a->id, 'normal');
        //Hide the "Order actions" meta box.
        remove_meta_box('woocommerce-order-actions', $a->id, 'side');
        //Hide the "Order notes" meta box.
        remove_meta_box('woocommerce-order-notes', $a->id, 'side');

      } 

        global $pagenow;

        if ( ( ! in_array($a->id, $pages) || $pagenow === 'post-new.php' ) ) {
          wp_redirect(get_site_url() . "/wp-admin/edit.php?post_type=product");
          die;
        } 
        
      }


    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Is $pages only defined when the following condition is true: $currentUserName === "testuser"? \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Mar 24 at 5:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.