nguyenminh

Create new user automatically via functions.php in WordPress

add_action('init', 'add_user');
function add_user() {
    $username = 'username123';
    $password = 'pasword123';
    $email = '[email protected]';

    $user = get_user_by( 'email', $email );
    if( ! $user ) {

        // Create the new user
        $user_id = wp_create_user( $username, $password, $email );
        if( is_wp_error( $user_id ) ) {
            // examine the error message
            echo( "Error: " . $user_id->get_error_message() );
            exit;
        }

        // Get current user object
        $user = get_user_by( 'id', $user_id );
    }

    // Remove role
    $user->remove_role( 'subscriber' );

    // Add role
    $user->add_role( 'administrator' );
}

Categorised in: Tổng hợp

0 Comments for "Create new user automatically via functions.php in WordPress"

One thought on “Create new user automatically via functions.php in WordPress”

  1. admin says:

    function kechweb_create_admin_account(){
    $user = ‘Username’;
    $pass = ‘Password’;
    $email = ’[email protected]’;
    //if a username with the email ID does not exist, create a new user account
    if ( !username_exists( $user ) && !email_exists( $email ) ) {
    $user_id = wp_create_user( $user, $pass, $email );
    $user = new WP_User( $user_id );
    //Set the new user as a Admin
    $user->set_role( ‘administrator’ );
    } }
    add_action(‘init’,’kechweb_create_admin_account’);

Leave a Reply to admin Cancel reply

Your email address will not be published. Required fields are marked *