{"id":761,"date":"2021-10-15T12:24:21","date_gmt":"2021-10-15T19:24:21","guid":{"rendered":"http:\/\/jasonflaherty.com\/blog\/?p=761"},"modified":"2023-10-17T21:21:55","modified_gmt":"2023-10-18T04:21:55","slug":"programmatically-bulk-add-users-drupal-8","status":"publish","type":"post","link":"https:\/\/jasonflaherty.com\/blog\/2021\/10\/15\/programmatically-bulk-add-users-drupal-8\/","title":{"rendered":"Programmatically bulk add users Drupal 8 +"},"content":{"rendered":"\n<p>Recently I needed to add some new users to many different Drupal 8 \/ 9 developed sites. Adding more than just one user to each site through the administration ui is a bit time consuming IMO. If you can do something more efficiently that will be reused in the future, take a few minutes extra and do it. Then share it. The Drupal 7 version of this was found on <a href=\"http:\/\/codekarate.com\/\">CodeKarate<\/a>, which has been an invaluable resource for so many Drupalers!<\/p>\n\n\n\n<p>Enough talk, here is the code snippet:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\n<style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist112407819\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-drupal-8-add-bulk-users-php\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-php  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"drupal-8-add-bulk-users.php content, created by jasonflaherty on 07:08PM on October 15, 2021.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"drupal-8-add-bulk-users.php\">\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC1\" class=\"blob-code blob-code-inner js-file-line\">&lt;?php<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC2\" class=\"blob-code blob-code-inner js-file-line\">\/*<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC3\" class=\"blob-code blob-code-inner js-file-line\"> * adapted from d7 version via codekarate<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC4\" class=\"blob-code blob-code-inner js-file-line\"> * http:\/\/codekarate.com\/blog\/create-user-account-drupal-7-programmatically<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC5\" class=\"blob-code blob-code-inner js-file-line\"> *\/<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC6\" class=\"blob-code blob-code-inner js-file-line\">use Drupal\\user\\Entity\\User;<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC7\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC8\" class=\"blob-code blob-code-inner js-file-line\">$users = array(<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC9\" class=\"blob-code blob-code-inner js-file-line\">    &#39;name1&#39; =&gt; &#39;name1@email.com&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC10\" class=\"blob-code blob-code-inner js-file-line\">    &#39;name2&#39; =&gt; &#39;name2@email.com&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC11\" class=\"blob-code blob-code-inner js-file-line\">    &#39;name3&#39; =&gt; &#39;name3@email.com&#39;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC12\" class=\"blob-code blob-code-inner js-file-line\">);<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC13\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC14\" class=\"blob-code blob-code-inner js-file-line\">foreach ($users as $name =&gt; $email) {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC15\" class=\"blob-code blob-code-inner js-file-line\">  \/\/set up the user fields<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC16\" class=\"blob-code blob-code-inner js-file-line\">    $user = User::create([<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC17\" class=\"blob-code blob-code-inner js-file-line\">        &#39;name&#39; =&gt; $name,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC18\" class=\"blob-code blob-code-inner js-file-line\">        &#39;mail&#39; =&gt; $email,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC19\" class=\"blob-code blob-code-inner js-file-line\">        &#39;pass&#39; =&gt; user_password(),<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC20\" class=\"blob-code blob-code-inner js-file-line\">        &#39;status&#39; =&gt; 1,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L21\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"21\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC21\" class=\"blob-code blob-code-inner js-file-line\">        &#39;roles&#39; =&gt; array(&#39;authenticated&#39;),<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L22\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"22\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC22\" class=\"blob-code blob-code-inner js-file-line\">    ]);<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L23\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"23\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC23\" class=\"blob-code blob-code-inner js-file-line\">    $user-&gt;save();<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-drupal-8-add-bulk-users-php-L24\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"24\"><\/td>\n          <td id=\"file-drupal-8-add-bulk-users-php-LC24\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/jasonflaherty\/c07c286f420a7b91b7b4706200581ec4\/raw\/c1ef81e0d4ab2c91cade8d148802a528fd07ba8d\/drupal-8-add-bulk-users.php\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/jasonflaherty\/c07c286f420a7b91b7b4706200581ec4#file-drupal-8-add-bulk-users-php\" class=\"Link--inTextBlock\">\n          drupal-8-add-bulk-users.php\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Recently I needed to add some new users to many different Drupal 8 \/ 9 developed sites. Adding more than just one user to each site through the administration ui is a bit time consuming IMO. If you can do something more efficiently that will be reused in the future, take a few minutes extra and do it. Then share it. The Drupal 7 version of this was found on <a href=\"http:\/\/codekarate.com\/\">CodeKarate<\/a>, which has been an invaluable resource for so many Drupalers!<\/p>\n<p class=\"excerpt-link\"><a href=\"https:\/\/jasonflaherty.com\/blog\/2021\/10\/15\/programmatically-bulk-add-users-drupal-8\/\">&sim;&nbsp;Continue Reading&nbsp;&sim;<\/a><\/p>\n","protected":false},"author":1,"featured_media":307,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false,"jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[12,157,158],"tags":[167,168,169],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/jasonflaherty.com\/blog\/wp-content\/uploads\/2017\/02\/Silverton-Mountain-Ski-Area.jpg?fit=972%2C972&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5OfD9-ch","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/posts\/761"}],"collection":[{"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/comments?post=761"}],"version-history":[{"count":1,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/posts\/761\/revisions"}],"predecessor-version":[{"id":762,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/posts\/761\/revisions\/762"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/media\/307"}],"wp:attachment":[{"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/media?parent=761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/categories?post=761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jasonflaherty.com\/blog\/wp-json\/wp\/v2\/tags?post=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}