arrow_back
Back

WordPress users: roles, capabilities, authentication, and user meta

Andrew Dorokhov Andrew Dorokhov schedule 3 min read
menu_book Table of Contents

Add a new user:

$user_id = wp_insert_user( [
    'user_login'    => sanitize_user( $username ),
    'user_email'    => sanitize_email( $user_email ),
    'user_pass'     => wp_generate_password(),
    'role'          => 'wp_job_board_pro_employer',
] );

Example from one theme:

$username = sanitize_title( $employer_name );

if ( strlen( $username ) > 60 ) {
    $username = substr( $username, 0, 60 );
}

if ( username_exists( $username ) ) {

    $username .= '_' . rand( 10000, 99999 );

    if ( username_exists( $username ) ) {
        $username .= '_' . rand( 10000, 99999 );
    }
}

$user_email = $username . '@fakeuser.com';

if ( email_exists( $user_email ) ) {
    $user_email = $username . '_' . rand( 10000, 99999 ) . '@fakeuser.com';

    if ( email_exists( $user_email ) ) {
        $user_email = $username . '_' . rand( 10000, 99999 ) . '_' . rand( 10000, 99999 ) . '@fakeuser.com';
    }
}

Getting the current user

A call to wp_get_current_user() always returns a WP_User object, even for a visitor who is not logged in, so check exists() before using the data:

$current_user = wp_get_current_user();

if ( ! $current_user->exists() ) {
    return;
}

echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';

Updating a user

wp_update_user( [
    'ID'         => $user_id,
    'user_email' => $email,
] );

Email addresses must stay unique, so check them before saving:

if ( email_exists( $email ) ) {
    $response['errors'][] = "This email is already in use.";
} else {
    wp_update_user( [
        'ID'         => $current_user->ID,
        'user_email' => $email,
    ] );
}

Adding fields to the profile screen

Custom fields can be added to the user profile with a pair of hooks: one to render the markup (show_user_profile for your own profile, edit_user_profile for someone else’s) and one to save it.

function ach_payments_user_profile_section( $user ) {
    ?>
    <h2>Subscription</h2>
    <table class="form-table">
        <tr>
            <th><label for="subscription-cost">Monthly Subscription Cost <span class="description">($)</span></label></th>
            <td>
                <input type="text" name="ach-payments-subscription-cost" id="subscription-cost" value="<?= esc_attr( get_user_meta( $user->ID, 'ach_payments_subscription_cost', true ) ) ?>">
                <p class="description">Please enter individual subscription cost.<br />
                    If you leave it empty, default subscription cost will be applied.<br />
                    Default subscription cost: <strong>$99</strong>.
                </p>
            </td>
        </tr>
    </table>
    <?php
}

add_action( 'show_user_profile', 'ach_payments_user_profile_section' );
add_action( 'edit_user_profile', 'ach_payments_user_profile_section' );
function ach_payments_user_profile_saving( $user_id ) {

    if ( ! current_user_can( 'edit_user', $user_id ) ) {
        return false;
    }

    if ( isset( $_POST['ach-payments-subscription-cost'] ) ) {
        update_user_meta( $user_id, 'ach_payments_subscription_cost', esc_attr( $_POST['ach-payments-subscription-cost'] ) );
    }
}

add_action( 'personal_options_update', 'ach_payments_user_profile_saving' );
add_action( 'edit_user_profile_update', 'ach_payments_user_profile_saving' );

Roles and capabilities

open_in_new Roles and Capabilities .

Roles

Adding a role:

add_role( 'marketer', 'Marketer', [
    'read' => true,
] );

Removing a role:

remove_role()

Capabilities

add_cap()
remove_cap() 

open_in_new https://www.role-editor.com/woocommerce-admin-bar-access/

code

Need Help with Development?

Happy to help — reach out via the contacts or go straight to my Upwork profile.

work View Upwork Profile arrow_forward
Next Article

WordPress meta boxes: add_meta_box, save_post, and post meta

arrow_forward