{"id":11086,"date":"2014-09-24T19:19:54","date_gmt":"2014-09-24T19:19:54","guid":{"rendered":"http:\/\/developer.wordpress.org\/?post_type=plugin-handbook&#038;p=11086"},"modified":"2022-11-17T06:08:51","modified_gmt":"2022-11-17T06:08:51","slug":"working-with-users","status":"publish","type":"plugin-handbook","link":"https:\/\/developer.wordpress.org\/plugins\/users\/working-with-users\/","title":{"rendered":"Working with Users"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Adding Users<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To add a user you can use <code>wp_create_user()<\/code> or <code>wp_insert_user()<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>wp_create_user()<\/code> creates a user using only the username, password and email parameters while <code>wp_insert_user()<\/code> accepts an array or object describing the user and its properties.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create User<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>wp_create_user()<\/code> allows you to create a new WordPress user.<\/p>\n\n\n\n<div class=\"wp-block-wporg-notice is-info-notice\">\n<div class=\"wp-block-wporg-notice__icon\"><\/div>\n<div class=\"wp-block-wporg-notice__content\"><br \/>\nIt uses <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_slash\/\" rel=\"function\">wp_slash()<\/a>  to escape the values. The PHP compact() function to create an array with these values. The <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_insert_user\/\" rel=\"function\">wp_insert_user()<\/a>  to perform the insert operation.<br \/>\n<\/div><\/div>\n\n\n\n\n<p class=\"wp-block-paragraph\">Please refer to the Function Reference about <code>wp_create_user()<\/code> for full explanation about the used parameters.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example Create<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">\/\/ check if the username is taken\n$user_id = username_exists( $user_name );\n\n\/\/ check that the email address does not belong to a registered user\nif ( ! $user_id &amp;&amp; email_exists( $user_email ) === false ) {\n\t\/\/ create a random password\n\t$random_password = wp_generate_password( 12, false );\n\t\/\/ create the user\n\t$user_id = wp_create_user(\n\t\t$user_name,\n\t\t$random_password,\n\t\t$user_email\n\t);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Insert User<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">wp_insert_user( $userdata );<\/code><\/pre>\n\n\n\n<div class=\"wp-block-wporg-notice is-info-notice\">\n<div class=\"wp-block-wporg-notice__icon\"><\/div>\n<div class=\"wp-block-wporg-notice__content\"><br \/>\nThe function calls a filter for most predefined properties.<\/p>\n<p>The function performs the action <code>user_register<\/code> when creating a user (user ID does not exist).<\/p>\n<p>The function performs the action <code>profile_update<\/code> when updating the user (user ID exists).<br \/>\n<\/div><\/div>\n\n\n\n\n<p class=\"wp-block-paragraph\">Please refer to the Function Reference about <code>wp_insert_user()<\/code> for full explanation about the used parameters.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example Insert<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Below is an example showing how to insert a new user with the website profile field filled in.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$username  = $_POST['username'];\n$password  = $_POST['password'];\n$website   = $_POST['website'];\n$user_data = [\n\t'user_login' =&gt; $username,\n\t'user_pass'  =&gt; $password,\n\t'user_url'   =&gt; $website,\n];\n\n$user_id = wp_insert_user( $user_data );\n\n\/\/ success\nif ( ! is_wp_error( $user_id ) ) {\n\techo 'User created: ' . $user_id;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Updating Users<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>wp_update_user()<\/code> Updates a single user in the database. The update data is passed along in the <code>$userdata<\/code> array\/object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To update a single piece of user meta data, use <code>update_user_meta()<\/code> instead. To create a new user, use <code>wp_insert_user()<\/code> instead.<\/p>\n\n\n\n<div class=\"wp-block-wporg-notice is-info-notice\">\n<div class=\"wp-block-wporg-notice__icon\"><\/div>\n<div class=\"wp-block-wporg-notice__content\"><br \/>\nIf current user&#8217;s password is being updated, then the cookies will be cleared!<br \/>\n<\/div><\/div>\n\n\n\n\n<p class=\"wp-block-paragraph\">Please refer to the Function Reference about <code>wp_update_user()<\/code> for full explanation about the used parameters.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example Update<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Below is an example showing how to update a user&#8217;s website profile field.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">$user_id = 1;\n$website = 'https:\/\/wordpress.org';\n\n$user_id = wp_update_user(\n\tarray(\n\t\t'ID'       =&gt; $user_id,\n\t\t'user_url' =&gt; $website,\n\t)\n);\n\nif ( is_wp_error( $user_id ) ) {\n\t\/\/ error\n} else {\n\t\/\/ success\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Deleting Users<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>wp_delete_user()<\/code> deletes the user and optionally reassign associated entities to another user ID.<\/p>\n\n\n\n<div class=\"wp-block-wporg-notice is-info-notice\">\n<div class=\"wp-block-wporg-notice__icon\"><\/div>\n<div class=\"wp-block-wporg-notice__content\"><br \/>\nThe function performs the action <code>deleted_user<\/code> after the user have been deleted.<br \/>\n<\/div><\/div>\n\n\n\n\n<div class=\"wp-block-wporg-notice is-alert-notice\">\n<div class=\"wp-block-wporg-notice__icon\"><\/div>\n<div class=\"wp-block-wporg-notice__content\"><br \/>\nIf the $reassign parameter is not set to a valid user ID, then all entities belonging to the deleted user will be deleted!<br \/>\n<\/div><\/div>\n\n\n\n\n<p class=\"wp-block-paragraph\">Please refer to the Function Reference about <code>wp_delete_user()<\/code> for full explanation about the used parameters.<\/p>\n","protected":false},"author":12560283,"featured_media":0,"parent":11084,"menu_order":0,"template":"","meta":{"footnotes":""},"class_list":["post-11086","plugin-handbook","type-plugin-handbook","status-publish","hentry","type-handbook"],"revision_note":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/plugin-handbook\/11086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/plugin-handbook"}],"about":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/types\/plugin-handbook"}],"author":[{"embeddable":true,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/users\/12560283"}],"version-history":[{"count":62,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/plugin-handbook\/11086\/revisions"}],"predecessor-version":[{"id":144325,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/plugin-handbook\/11086\/revisions\/144325"}],"up":[{"embeddable":true,"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/plugin-handbook\/11084"}],"wp:attachment":[{"href":"https:\/\/developer.wordpress.org\/wp-json\/wp\/v2\/media?parent=11086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}