Dorokhov.codes

18. Working with users

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';
    }
}

Roles and capabilities

Roles and Capabilities.

Roles

Adding a role:

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

Removing a role:

remove_role()

Capabilities

add_cap()
remove_cap() 

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