Search code examples
phpwordpresswoocommerceregistrationuser-roles

Showing WordPress backend to WooCommerce customer user role


Maybe someone knows how to link the registration WooCommerce and registration wordpress?

For example the user should have the ability to publish posts, etc. The problem is that the standard admin panel hides when user login.


Solution

  • This is for WooCommerce Customer user role only:

    Here you have the requisites hooked functions to do what you are looking at. This will allow customer user role to access to wordpress backend and to publish/edit posts (with this last feature you should take care as customer user role will have the ability to add/edit/publish posts, so backup your database before).

    Here is the code:

    add_filter('woocommerce_disable_admin_bar', '_wc_disable_admin_bar', 10, 1);
    add_filter('woocommerce_prevent_admin_access', '_wc_prevent_admin_access', 10, 1);
    function _wc_prevent_admin_access($prevent_admin_access) {
        $user_data = get_userdata( get_current_user_id() );
        $user_roles = $user_data->roles;
        $customer_role = get_role( 'customer' );
    
        // For "customer" WooCommerce user role only
        if (in_array('customer', $user_roles)) {
    
            // Warning! with this (This will be definitive, so make a database backup)
            // Adding 'add_post', 'publish_posts' and 'edit_post' capabilities to customer user role
            if ( !user_can( get_current_user_id(), 'publish_posts' ) ){
                $customer_role->add_cap( 'create_posts' );
                $customer_role->add_cap( 'publish_posts' );
                $customer_role->add_cap( 'edit_posts' );
            }
    
            // Giving access to wordpress backend
            return false;
        }
    }
    

    The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested annd works