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);
  • the action [profile_update]() is only fired when a user is updated in the dashboard. You need to hook your function on init instead. – Aurovrata Sep 4 '18 at 7:05
  • @Aurovrata I've just tried init but doesn't work. I don't see "author" role in the user account after a user clicked the apply button. – Alief Sep 4 '18 at 7:29
  • well likely you have an error in your code. Are you getting a value in your submitted post $_POST['author_checkbox'] – Aurovrata Sep 4 '18 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.