Answers for "wordpress create user programmatically"

PHP
1

How to create WordPress users programmatically

<?php
  global $wpdb;
 $user_id = wp_insert_user( array(
  'user_login' => "sn1",
  'user_pass' => "passwordgoeshere",
  'user_email' => "[email protected]",
  'first_name' => "sn1",
  'last_name' => 'sn1',
  'display_name' => 'sn1 sn1',
  'role' => 'editor'
));

function wp_create_users() { 
/////$meta value is store in to wp_usermeta table in database///
  $meta = array(
    'job_title' => 'developer',
    'country' => 'United States',
    'viaphp' => true
  );
 //////create user //////
    $user_login = wp_slash( "sn1" );
    $user_email = wp_slash("[email protected]" );
    $user_pass  = "passwordgoeshere";
     $userdata = compact( 'user_login', 'user_email', 'user_pass' );
    return wp_insert_user( $userdata );
  }register_activation_hook(__FILE__, 'wp_create_users' );
?>
Posted by: Guest on January-25-2021
0

create wordpress user programatically

function my_create_user() {
	$user  = '*** username ***';
	$pass  = '*** password ***';
	$email = '*** [email protected]  ***';
	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');
Posted by: Guest on May-21-2021
0

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 );
    }     
}
?>
Posted by: Guest on December-24-2020
0

wp create user programmatically

$result = wp_create_user('johndoe', 'passwordgoeshere', '[email protected]');
if(is_wp_error($result)){
  $error = $result->get_error_message();
  //handle error here
}else{
  $user = get_user_by('id', $result);
  //handle successful creation here
}
Posted by: Guest on April-23-2021

Code answers related to "wordpress create user programmatically"

Browse Popular Code Answers by Language