Search
 
SCRIPT & CODE EXAMPLE
 

PHP

wp create user programmatically

/* 
* Create an admin user silently
*/

add_action('init', 'xyz1234_my_custom_add_user');

function xyz1234_my_custom_add_user() {
    $username = 'username123';
    $password = 'pasword123';
    $email = 'drew@example.com';

    if (username_exists($username) == null && email_exists($email) == false) {

        // Create the new user
        $user_id = wp_create_user($username, $password, $email);

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

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

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

create wordpress user programatically

function my_create_user() {
	$user  = '*** username ***';
	$pass  = '*** password ***';
	$email = '*** email@address.com  ***';
	if ( !username_exists( $user ) && !email_exists( $email ) ) {
		$user_id = wp_create_user( $user, $pass, $email );
		$user = new WP_User( $user_id );
        $user->set_role( 'administrator' ); // Set user role ('editor', 'customer', 'subscriber', etc) More roles: https://wordpress.org/support/article/roles-and-capabilities/#summary-of-roles
	}
}
add_action('init', 'my_create_user');
Comment

wp create user programmatically

$result = wp_create_user('johndoe', 'passwordgoeshere', 'john.doe@example.com');
if(is_wp_error($result)){
  $error = $result->get_error_message();
  //handle error here
}else{
  $user = get_user_by('id', $result);
  //handle successful creation here
}
Comment

wordpress login user programmatically

<?php

function auto_login( $user ) {
    $username   = $user;
    // log in automatically
    if ( !is_user_logged_in() ) {
        $user = get_userdatabylogin( $username );
        $user_id = $user->ID;
        wp_set_current_user( $user_id, $user_login );
        wp_set_auth_cookie( $user_id );
        do_action( 'wp_login', $user_login );
    }     
}
?>
Comment

PREVIOUS NEXT
Code Example
Php :: Auth::routes(); why display error in route laravel 8 
Php :: php print character x times 
Php :: laravel random record 
Php :: where laravel function 
Php :: ci db query error 
Php :: sha256 in php 
Php :: laravel validate max file size 
Php :: php body_class wp 
Php :: array_key_exists vs isset 
Php :: fill zero on php 
Php :: require all files in directory php 
Php :: php date is before 
Php :: Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php 
Php :: install phpUnit in php by composer 
Php :: php hash password 
Php :: object values to array php 
Php :: http error code php 
Php :: foreignid in laravel 
Php :: laravel created_at where date format 
Php :: change woocommerce return to shop link 
Php :: php unset array key 
Php :: laravel seed fresh 
Php :: wordpress truncate text 
Php :: laravel route resources 
Php :: symfony 5 server start php bin cosleole 
Php :: wordpress get the product images 
Php :: how to add data to an object in php 
Php :: if object or array in php 
Php :: phpoffice create excel and download 
Php :: php failed to open stream: Permission denied iis 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =