2010-11-16 359 views

回答

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";}') 

你需要裸露記住幾件事情:

  • 的用戶名必須是唯一的
  • 密碼必須是與 相關wp_hash_password()
  • 第二個語句 中的user_id是第一個的insert_id聲明。

下面是一些示例代碼:

<?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'] = "[email protected]"; 
     $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); 
?> 

數據庫對象可以在這裏找到:

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

+0

2) – James 2010-11-16 21:43:29

+0

千恩萬謝了一些bug,如果你充分合作例子,好像如果我們談論與登錄「詹姆斯創造新的管理員「 – James 2010-11-16 21:44:42

+0

我修復了這個錯誤並添加了一些代碼。 – 2010-11-16 21:53:14

0

詹姆斯的代碼示例,並沒有爲我工作,所以我寫了相似的東西。把這段代碼放在一個文件中,比如說你的wordpress/wp-includes目錄下的createhash.php。

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

更改「您的密碼」您的密碼,然後鍵入php createhash.php,它應該打印出你的密碼的密碼哈希。

乾杯,戴夫。

1

我最近不得不創建管理員用戶從命令行許多WordPress網站,我創造了這個腳本,您可以從phpMyAdmin的或shell命令行中運行: 所有你需要做的是配置用戶名,密碼和電子郵件運行。

簡單的版本硬編碼 「wp_prefix」 到 「WP_」

創建WordPress管理用戶:使用預處理語句

SET @username = 'YOUR_USERNAME'; 
SET @password = 'YOUR_PASSWORD'; 
SET @email = '[email protected]'; 
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 = '[email protected]'; 
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; 
+0

小心!你使用MD5而不用任何鹽來散列密碼。這使得它很容易破解。考慮使用'wp_hash_password()'來代替。 – aaronk6 2015-07-22 07:08:34

+0

感謝您的「創建管理員用戶」!關於上面的評論....你意識到'wp_hash_password()'不是MySQL的功能嗎? – Sam 2016-12-06 17:17:52