Skip to content

Instantly share code, notes, and snippets.

@ahmad24
Created December 7, 2013 08:51
Show Gist options
  • Save ahmad24/7838746 to your computer and use it in GitHub Desktop.
Save ahmad24/7838746 to your computer and use it in GitHub Desktop.
wordpress : insert user
<?php
/*
Plugin Name: Insert User
Plugin URI: http://example.com
Description: Plugin that inserts a user.
Version: 0.1
Author: WROX
Author URI: http://goo.com
*/
/* Insert the new user on the 'init' hook. */
add_action( 'init', 'boj_insert_user' );
/* Inserts a new user. */
function boj_insert_user() {
/* Do nothing if the 'wrox' username exists. */
if ( username_exists( 'wrox' ) )
return;
/* Set up the user data. */
$userdata = array(
'user_login' => 'wrox',
'user_email' => 'wrox@example.com',
'user_pass' => '123456789',
'user_url' => 'http://example.com',
'display_name' => 'Wrox',
'description' => 'Loves to publish awesome books on WordPress!',
'role' => 'editor'
);
// *** wp_create_user( $username, $password, $email ); **
// This function enables you to insert the minimum arguments for creating a user, which simplifies
// creating users greatly. It's especially useful if you don't have any other data you want to input.
/* Create the user. */
$user = wp_insert_user( $userdata );
/* If the user wasn't created, display the error message. */
if ( is_wp_error( $user ) )
echo $result-> get_error_message();
}
/* Get the user data. */
function boj_display_user_website( $user_id ) {
/* Get the user data. */
$data = get_userdata( $user_id );
/* Check if data was returned. */
if ( !empty( $data ) ) {
/* Check if a URL has been given. */
if ( !empty( $data->user_url ) ) {
/* Display the user display name and URL. */
echo $data->display_name . ': ' . $data->user_url;
}
}
}
/* Display user welcome message in the admin footer. */
add_action( 'in_admin_footer', 'boj_user_welcome_message' );
function boj_user_welcome_message() {
// The get_currentuserinfo() function is similar to the function from the next section.
// wp_get_current_user() actually calls it to get the user's information. The big difference between
// the two functions is that get_currentuserinfo() doesn't return a variable. It sets a global variable
// of $current_user instead.
/* Get the current user's data. */
$data = wp_get_current_user();
/* Display a message for the user. */
echo "Hello, {$data->display_name}. < br />";
}
//***************get_currentuserinfo***************
add_action( 'in_admin_footer', 'boj_display_user_registration_date' );
function boj_display_user_registration_date() {
/* Globalize the $current_user variable. */
global $current_user;
/* Call the current user function. */
get_currentuserinfo();
/* Format user registration date. */
$date = mysql2date( 'F j, Y', $current_user->user_registered );
/* Display the user's registration date. */
echo "You registered on {$date}. < br />";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment