WordPress

Working with User management in WordPress

A user is generally a person who does certain set of work on any system. A typical Blogging platform or CMS will have certain level of users and hence user management is an important aspect, where there are many and different users. WordPress too handles this and takes care of most of the functionality that would be required. WordPress generally keeps user data in “wp_user” table and saves meta values related to users in “wp_usermeta” table.

While “wp_user” contains the username, password, full name, email and a few more data about the user, “wp_usermeta” contains other information that are not covered by the main table. “wp_usermeta” is similar to “wp_postmeta” and contains data in ‘key’, ‘value’ format. In this article I will discuss the basic functions related to users and probably discuss their capabilities in another article which would probably come this week. The main functions relating to user management are:

 

wp_create_user  function is used when you are creating a user with minimal data, i.e. username, password and email. Though this and wp_insert_user will both add user to the table, wp_create_user is used to add users with minimal data whereas wp_insert_data accepts array of data and is used when more than just email, username and password are to be inserted.

 

Example: wp_create_user
add_action( 'init', 'create_new_euser' );
function create_new_user() {
wp_create_user( "username", "password", "some@email.com" );
}

 

Example: wp_insert_user

add_action( 'init', 'insert_new_user' );
function insert_new_user() {
// Prepare the user data
$user_data = array (
'user_login' => 'username',
'user_password' => 'password'
'user_email' => 'some@email.com',
'role' => 'author'
);

wp_insert_user( $user_data );

}

 

Example: wp_delete_user

add_action( 'init', 'delete_new_user' );
function delete_new_user() {
wp_delete_user( 1 ); // where 1 is the user's ID to be deleted.
}

 

Example: wp_update_user

add_action( 'init', 'update_new_user' );
function update_new_user() {
// Prepare the user data
$user_data = array (
'user_id' => 1,
'email' => 'somethingnew@email.com'
);
wp_update_user( $user_data );
}

*Note: I haven’t done the checking here. You would better check if the data are inserted and show message accordingly.

 

So that gives the basic idea of how users can be handled using WordPress. In my continuation to this article. I shall discuss about the User Meta and how they can be used to store additional information about the user. Hope this was of help.