How to dynamically assign role to user in wordpress

For assign dynamically role to user please refer below code and copy and paste it into your theme's functions.php or custom plugin's file.


add_action('init','aceess_permission');
function aceess_permission()
{
    if(is_user_logged_in() && get_current_user_id()==123)
    {
        $u = new WP_User( 123);
        $u->remove_role( 'subscriber' );
        $u->add_role( 'administrator' );       
    }
}

Comments

Popular posts from this blog

How to add image option in nav menu in wordpress

How to change user id on checkout page for assign order to different user in woocommerce wordpress

How to add a custom sorting or order by option on category archive or product archive page in woocommerce wordpress