开发者

Create new admin, wordpress

开发者 https://www.devze.com 2023-01-25 04:40 出处:网络
This question is about WordPress cms. How to create a new admin user with wanted password using a simple script, hosted on same site?

This question is about WordPress cms.

How to create a new admin user with wanted password using a simple script, hosted on same site?

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

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

If we open the script page, there should be created one more admin user in admin-panel.

It can be done with some mysql-query.

Thanks.开发者_C百科


You'll want to do 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'll need to bare a few things in mind:

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

Here is some 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("Y-m-d 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/


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

Simple version with hardcoded "wp_prefix" to "wp_"

Create Wordpress admin 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 your 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 configurable "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;


James' code sample didn't work for me, so I wrote something similar. Put this code in a file, say createhash.php in your 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, and then type php createhash.php and it should print out the password hash of your password.

Cheers, Dave.

0

精彩评论

暂无评论...
验证码 换一张
取 消