Add another role to a user when they click a button?

On my site, there are roles “user, authors, etc.”. Default role for user is “User” which I want to keep it for all users.

How to add another role to a user when they click a button?

I tried with my code below but it doesn’t work.

I have a page “Create a Post” that I want to restrict it to authors only. So, when a normal logged-in user tries to access the “Create a Post” page, they will be present with a form to apply as Author which they only need to click the button.

After they click “Apply” button, they will immediately get the Author role and immediately have access to the frontend post editor in the “Create a Post” page.

Here’s my code:

add_shortcode( 'apply_as_author', 'apply_as_author_button_shortcode' );

function apply_as_author_button_shortcode() {
if(is_user_logged_in()) {
$user = wp_get_current_user();
<form method="POST">
<div style="text-align:center;">
    <h3 style="margin:auto;">Apply to be an author?</h3>
    <input checked="checked" type="hidden" class="input-checkbox" name="author_checkbox" id="author_checkbox" value="<?php echo esc_attr( $user->author_checkbox ); ?>" />
    <br />
    <span style="font-size:16px;margin:auto;">
    Your application to be an author will be approved immediately right after you click the apply button.<br />
    As an author, share your best stories and new ideas, and get discovered by many!
    <input type="submit" class="apply-author-button" name="submit" value="<?php esc_attr_e( 'Apply', 'wordpress' ); ?>" />
    <input type="hidden" name="action" value="submit" />

add_action('profile_update', 'role_option_update_user_meta');
function role_option_update_user_meta( $user_id ) {
if ( isset($_POST['author_checkbox']) ) {
    $user_id = wp_update_user( array( 'ID' => $user_id, 'role' => $_POST['role'] ) );
    if( $user_id > 0 ){
        $user = new WP_User($user_id);


New User Asked on September 19, 2018 in Customizations.
Add Comment
1 Answer(s)

the action [profile_update]() is only fired when a user is updated in the dashboard. You need to hook your function on init instead.

New User Answered on September 19, 2018.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service. | Learn how to add/edit code.