On this page
- 1 Creating an Administrator Account in WordPress Using Custom PHP Code
- 2 Creating an Administrator Account in WordPress Using Custom PHP Code
- 2.1 Conclusion
- 2.1.0.1 Metadata in WordPress
- 2.1.0.2 WP_List_Table tutorial : How to display data from database to the admin block ?
- 2.1.0.3 Shortcode in WordPress
- 2.1.0.4 Add Tracking Code in WordPress
- 2.1.0.5 WordPress: Post Types
- 2.1.0.6 WordPress: Plugins
- 2.1.0.7 WordPress: Hooks
- 2.1.0.8 WordPress: Important Functions
- 2.1.0.9 Cheatsheet: WordPress
- 2.2 Leave A Comment Cancel reply
- 2.1 Conclusion
Creating an Administrator Account in WordPress Using Custom PHP Code
Creating an Administrator Account in WordPress Using Custom PHP Code
August 21, 2020
In WordPress development, there are times when you might need to programmatically create an administrator account for a site. This can be useful for setting up environments, automating deployments, or managing user roles during development. Below is a breakdown of a PHP code snippet designed to create a new WordPress administrator account.
<?php function wpb_admin_account(){ $user = 'johndoe'; $pass = 'Password'; $email = 'email@domain.com'; if ( !username_exists( $user ) && !email_exists( $email ) ) { $user_id = wp_create_user( $user, $pass, $email ); $user = new WP_User( $user_id ); $user->set_role( 'administrator' ); } } add_action('init','wpb_admin_account'); ?>
Conclusion
This PHP snippet provides a straightforward method to create an administrator account programmatically in WordPress. By integrating this code into your theme or plugin, you can automate user setup and streamline administrative tasks. Always remember to handle sensitive data with care and test thoroughly to avoid security risks.