I have a blog with different user roles and therefore different user permission levels.

From time to time, I would like to give my registered users the possibility to upgrade their current user role ("subscriber" to "premiummember") for free, but I don't want to do this for every user who wants to be upgraded manually.

I thought that it would be nice to have a little plugin (running via shortcode ([upgrade_user_role] + [request_user_role]) which allows my users to upgrade their user role by a simple click of a button and furthermore also gives them the opportunity to request their current user role status.

I don't know how to code this. But at least I can try and explain to you how the plugin should work.

  1. At first it should check if the user is already logged in and if so, it should load the CSS file with the button layout.
  2. Now that the user is logged in they should be able to upgrade or request their current user role by simply clicking one of the buttons.

I really tried to code this by myself, but I couldn't manage to get it to work properly.

For the login check I tried to use the is_user_logged_in() function in combination with an simple if query. For the update and request roles functions, I tried to use:

$current_user = wp_get_current_user();

As well as

$current_user_id = $current_user->ID;

In combination with

update_metadata('user', $current_user_id, 'role', '$current_role)

Edit: I tried to code a little plugin with the following code underneath, but i can't manage to get the form being displayed when using the setup [show_upgrade_to_premium] in a post or site.

function upgrade_to_premium() {
    if(is_user_logged_in()) {
        if(is_page('upgrade-to-premium')) {
            global $load_css;
            $load_css = true;
            $output = upgrade_to_premium_form();
            $current_user = wp_get_current_user();
            if( $current_user->roles[0] == "subscriber" || $current_user->roles[0] == "premium" ) {
                $user_id = $current_user->id;
                $role = $current_user->roles[0];
                if( $_POST['role']){
                    if( $_POST['role'] == $role ) {
                        echo "Sorry, you are already " . $role . "!";
                    } else {
                        $role = $_POST['role'];
                        $userdata = array();
                        $userdata['ID'] = $user_id;
                        $userdata['role'] = $role;
                        echo "Your user type has been changed!  You are now " . $role . "!";
    else {

    return $output;
add_shortcode('show_upgrade_to_premium', 'upgrade_to_premium');

function upgrade_to_premium_form() {
    ob_start(); ?>
    <h3 class="header"><?php _e('Upgrade to premium'); ?></h3>
    <form id="upgrade_to_premium_form" class="form" action="" method="POST">
        Please select a role:<br/>
        <select name="role">
            <option value="subscriber" selected="selected">Subscriber</option>
            <option value="premium">Premium</option>
        <input TYPE="submit" name="submit" />
    return ob_get_clean();

function register_css() {
    wp_register_style('form-css', plugin_dir_url( __FILE__ ) . '/css/forms.css');
add_action('init', 'register_css');

function print_css() {
    global $load_css;

    if ( ! $load_css )

add_action('wp_footer', 'print_css');

1 Answer 1


To update a user's role, here's a sample code.

$user_id = get_current_user_id();

$user = new WP_User( $user_id );
$user->set_role( 'my_new_role' );

Hope that helps

  • I appreciate your help but i can't get it to work...
    – JulesB
    Sep 19, 2016 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.