Create a new admin, wordpress

This question is about WordPress cms.

How to create a new admin user with the desired password using a simple script located on the same site?

Like, we have the site http://example.com/

And the script is http://example.com/ourscript.php

If we open the script page, another administrator must be created in the admin panel.

This can be done with some mysql query.

Thanks.

+4
source share
3 answers

You need to fulfill two queries:

1.) insert into wp_users values('','admin_username','user_password','admin','user_email','user_url','registration_date','',0,'admin') 2.) insert into wp_usermeta values ('','user_id','wp_capabilities','a:1:{s:13:"administrator";s:1:"1";}') 

You need to learn a few things:

  • username must be unique
  • password must be hashed using wp_hash_password ()
  • user_id in the second expression is the insert_id of the first expression.

Here is a sample code:

 <?php //get required support files require("database.class.php"); require("wordpress/functions.php"); //build the first insert array and send $array['user_login'] = "james"; $array['user_pass'] = wp_hash_password("test"); $array['user_nicename'] = "James"; $array['user_email'] = " me@jtgraphic.net "; $array['user_url'] = "http://jtgraphic.net"; $array['user_registered'] = date("Ymd H:i:s"); $array['user_status'] = 0; $array['display_name'] = "James"; $user_id = $database->insert("wp_users",$array); //build the second insert array and send $array['user_id'] = $user_id; $array['meta_key'] = "wp_capabilities"; $array['meta_value'] = "a:1:{s:13:"administrator";s:1:"1";}"; $database->insert("wp_usermeta",$array); ?> 

The database object can be found here:

http://www.jtgraphic.net/code/database-object/

+4
source

I recently had to create admin users for many Wordpress sites from the command line, and I created this script that can be run either from the phpMyAdmin command line or from the shell command line: All you need to do is set up your username, password and email mail and run it.

A simple version with the hard code "wp_prefix" to "wp _"

Create admin Wordpress user:

 SET @username = 'YOUR_USERNAME'; SET @password = 'YOUR_PASSWORD'; SET @email = ' YOUR@EMAIL.COM '; INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`) VALUES (@username, MD5(@password), @username, @email, '0'); SET @userid = LAST_INSERT_ID(); INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, @userid, 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}'); INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, @userid, 'wp_user_level', '10'); 

Delete user

 SET @username = 'YOUR_USERNAMEโ€‹'; SET @userid := (SELECT `ID` FROM `wp_users` WHERE `user_login` = @username); DELETE FROM `wp_users` WHERE `ID` = @userid LIMIT 1; DELETE FROM `wp_usermeta` WHERE `user_id` = @userid LIMIT 2; 

More advanced version using prepared statements and custom wp_prefix

Create admin user

 SET @username = 'YOUR_USERNAME'; SET @password = 'YOUR_PASSWORD'; SET @email = ' YOUR@EMAIL.COM '; SET @wp_prefix = 'MY_WP_PREFIX_'; SET @sql_user = CONCAT('INSERT INTO `',@wp_prefix,'users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`) VALUES ("',@username,'", MD5("',@password,'"), "',@username,'", "',@email,'", "0")'); PREPARE sql_user FROM @sql_user; EXECUTE sql_user; DEALLOCATE PREPARE sql_user; SET @userid = LAST_INSERT_ID(); SET @wp_usermeta1 = CONCAT("INSERT INTO `",@wp_prefix,"usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, ?, '",@wp_prefix,"capabilities', 'a:1:{s:13:\"administrator\";s:1:\"1\";}')"); PREPARE wp_usermeta1 FROM @wp_usermeta1; EXECUTE wp_usermeta1 USING @userid; DEALLOCATE PREPARE wp_usermeta1; SET @wp_usermeta2 = CONCAT('INSERT INTO `',@wp_prefix,'usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, "',@userid,'", "',@wp_prefix,'user_level", "10")'); PREPARE wp_usermeta2 FROM @wp_usermeta2; EXECUTE wp_usermeta2; DEALLOCATE PREPARE wp_usermeta2; 

Delete user

 SET @username = 'YOUR_USERNAME'; SET @wp_prefix = 'MY_WP_PREFIX_'; SET @sql_userid = CONCAT('SELECT @userid := `ID` FROM `',@wp_prefix,'users` WHERE `user_login` = "',@username,'"'); PREPARE sql_userid FROM @sql_userid; EXECUTE sql_userid; DEALLOCATE PREPARE sql_userid; SET @wp_users = CONCAT('DELETE FROM `',@wp_prefix,'users` WHERE `ID` = "',@userid,'" LIMIT 1'); PREPARE wp_users FROM @wp_users; EXECUTE wp_users; DEALLOCATE PREPARE wp_users; SET @wp_usermeta = CONCAT('DELETE FROM `',@wp_prefix,'usermeta` WHERE `user_id` = "',@userid,'" LIMIT 2'); PREPARE wp_usermeta FROM @wp_usermeta; EXECUTE wp_usermeta; DEALLOCATE PREPARE wp_usermeta; 
+1
source

The James code example did not work for me, so I wrote something like this. Put this code in a file, say createhash.php in the wordpress / wp-includes directory.

 <?php define("ABSPATH", "../"); define("WPINC", ""); require("pluggable.php"); print(wp_hash_password("Your password") . "\n"); 

Change โ€œYour Passwordโ€ to your password, then enter php createhash.php and it should print the password hash of your password.

Greetings, Dave.

0
source

All Articles