Woocommerce blocking custom role to access admin_bar/admin_access


Developed a plugin which will add_role() called backend_user during plugin activation. In the plugin I had designed a controller which enables access to certain pages of ‘my custom plugin menu’ for backend_user. So whenever the user(role:backend_user) logins in via wp-admin they can see admin bar and access the ‘My custom plugin’ menus, this user is set to level_0 => true, read => true and nothing else.

Now the issuse is with the WooCommerce user controls, by default WooCommerce disable access to custom roles and in this case backend_user. So used the WooCommerce documentation and for the time gap added following filter in functions.php which works as expected, but I need a approach to make this filter coded inside the plugin and make it work during plugin activation. Any help would be grateful.

Filter used in functions.php:

if (current_user_can( 'backend_user' )) 
add_filter( 'woocommerce_prevent_admin_access', '__return_false' );
add_filter( 'woocommerce_disable_admin_bar', '__return_false' );

My custom plugins Activate.php:

 * @package  My custom Plugin
namespace IncBase;

class Activate
    public static function activate() 


        $default = array();

        if (! get_option( 'my_plugin' )) {
            update_option('my_plugin', $default );

// Add Bakend role
            $result =
            add_role ( 'backend_user', __( 'My Plugin user', 'testsite' ),
                    'read' => true,
                    'level_0'=> true,

, , , Bala Krishnan D 4 years 2020-05-26T13:10:33-05:00 0 Answers 92 views 0

Leave an answer