• How To?
  • Tips ‘n Tricks
  • WordPress
  • Snippets
  • Software
    • Browsers
    • Downloads
  • Web
  • Tools
    • Character Counter
    • chmod Calculator
    • Entities Encoder
    • Live HTML Editor
    • My IP
  • Contact
Twitter Facebook Google+ RSS
You are here: SumTips » Snippets » WordPress Snippets » Create New User in WordPress Using functions.php

Create New User in WordPress Using functions.php

Posted on September 15, 2011 by Renji | Short URL: http://sumtips.com/?p=5049

Here’s a little snippet for WordPress that will let you create a new user using just the functions.php file.

Just paste the below code, with the new user credentials, into your current theme’s functions.php file and WordPress will automatically create a new account.

function add_admin_acct(){
        $login = 'acctname'; //Username
        $passw = 'acctpass'; //Password
        $email = 'acctmail@domain.com'; //Account Email

        if ( !username_exists( $login )  && !email_exists( $email ) ) {
                $user_id = wp_create_user( $login, $passw, $email );
                $user = new WP_User( $user_id );
                $user->set_role( 'administrator' ); //Alternatives 'subscriber', 'editor', 'author', 'contributor'
        }
}
add_action('init','add_admin_acct');

This code will create a new administrator account, but you can as easily create users with other roles.

If an account with the username or email address specified already exists in the database, it will silently fail.


Tweet

Related posts:

  • Set Different Favicons for Frontend, Backend and Login Page on WordPress
  • How to Create a Subdomain and Install WordPress on it?
  • Run Windows Applications under Different User Credentials with RunAs
  • Hide WordPress Update Nag in Admin Dashboard
  • Twitter User Name @Israel Sold for Six Figures to Israel
Categories: WordPress Snippets
Delete Akismet and Spam Comments Metadata

  • Get Updates via Email

  • Recent Posts

    • CMS2CMS: Migrate Site from Drupal or Joomla to WordPress
    • WordPress: Add Preview Button in Distraction Free Writing
    • How to Activate or Deactivate Individual Jetpack Modules
    • Windows 8: Auto Update Defender with Windows Update Disabled
    • Automatically Start and Close Programs at Specific Time
    • How to Copy Code from Notepad++ with Syntax Highlighting
  • Random Posts

    • 25 Years of Microsoft Excel [Videos]
    • Search & Find Old Facebook Status Updates, Wall posts and More
    • Create Interactive Image Maps with Thinglink
    • Install a Portable Web Server on your USB Pen drive
    • iTunes 10.1 Released for Mac / Windows with AirPlay, iOS 4.2 Support
    • Compare, Find Difference Between Files Online
  • Categories

    • Blogging
    • Games
    • Google
    • How To?
    • Linux
    • Microsoft
      • Windows
    • Miscellaneous
    • Phone
    • Snippets
      • AutoHotkey Snippets
      • CSS Snippets
      • htaccess Snippets
      • JavaScript Snippets
      • PowerShell Snippets
      • WordPress Snippets
    • Social
    • Software
      • Browsers
    • Tips 'n Tricks
    • Wallpapers
    • Web
© SumTips. Contact | Sitemap | Privacy Policy