这个问题是关于WordPress cms。
如何使用托管在同一网站上的简单脚本创建具有所需密码的新管理员用户?
就像,我们有网站http://example.com/
脚本http://example.com/ourscript.php
如果我们打开脚本页面,应该在admin-panel中再创建一个管理员用户。
可以使用一些mysql查询来完成。
感谢。
答案 0 :(得分:4)
您需要进行两次查询:
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";}')
你需要记住一些事情:
以下是一些示例代码:
<?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);
?>
可在此处找到数据库对象:
答案 1 :(得分:2)
我最近不得不从命令行为许多Wordpress站点创建管理员用户,我创建了这个脚本,你可以从phpMyAdmin或shell命令行运行: 您需要做的就是配置用户名,密码和电子邮件并运行它。
简单版本,硬编码“wp_prefix”改为“wp _”
创建Wordpress管理员用户:
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');
删除您的用户
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;
使用预准备语句和可配置“wp_prefix”的更高级版本
创建管理员用户
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;
删除用户
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;
答案 2 :(得分:0)
詹姆斯的代码示例对我不起作用,所以我写了类似的东西。将此代码放在一个文件中,比如在wordpress / wp-includes目录中创建createhash.php。
<?php
define("ABSPATH", "../");
define("WPINC", "");
require("pluggable.php");
print(wp_hash_password("Your password") . "\n");
将“您的密码”更改为您的密码,然后输入php createhash.php
,它应该打印出您密码的密码哈希值。
干杯,戴夫。