Important: The code posted below is not fully tested. There are lots of area where you can improve the code as this is only to show you the right way. If you need any pointer, check user-new.php as the reference.
我将使用插件的类结构,假设插件名称为
create-user
. 因此,该插件的基本结构将是:
<?php
/*
Plugin Name: Create User
Description: Create user
Plugin URI: ****
Author: sakibmoon
Author URI: ****
Version: 0.0.1
*/
class create_user{
}
首先,我们必须创建管理菜单页面。我们将在构造函数中添加它。
public function __construct(){
//Action hook to add menu option in the admin panel
add_action(\'admin_menu\', array( &$this, \'add_menu\') );
}
The
add_menu()
功能如下
public function add_menu(){
//Creating top level menu as staff
//Use add_submenu_page for adding additional submenu
add_menu_page( \'Staff\', \'staff\', \'edit_users\', __FILE__, array( &$this, \'show_staff_page\' ) );
}
show_staff_page()
函数将显示页面内容。我们将显示表单以创建用户作为页面内容。我已经从
user-new.php
角色除外。默认情况下,wordpress使用
wp_dropdown_roles()
填充下拉字段。但如果你想排除
Administrator
你不能直接打电话。您必须删除
Administrator
从…起
editable_roles
. 为了节省时间,我对它进行了硬编码。但我认为最好的选择是使用过滤器
editable_roles.
How to remove administrator role in settings -> general -> New User Default Role? 会给你一些关于这个的想法。
public function show_staff_page(){
?>
<?php //Input to create new User ?>
<form action="" method="post" name="adduser" id="adduser" class="validate"<?php do_action(\'user_new_form_tag\');?>>
<input name="action" type="hidden" value="adduser" />
<?php wp_nonce_field( \'add-user\', \'_wpnonce_add-user\' ) ?>
<?php
// Load up the passed data, else set to a default.
foreach ( array( \'user_login\' => \'login\', \'first_name\' => \'firstname\', \'last_name\' => \'lastname\',
\'email\' => \'email\', \'url\' => \'uri\', \'role\' => \'role\', \'send_password\' => \'send_password\', \'noconfirmation\' => \'ignore_pass\' ) as $post_field => $var ) {
$var = "new_user_$var";
if( isset( $_POST[\'createuser\'] ) ) {
if ( ! isset($$var) )
$$var = isset( $_POST[$post_field] ) ? wp_unslash( $_POST[$post_field] ) : \'\';
} else {
$$var = false;
}
}
?>
<table class="form-table">
<tr class="form-field form-required">
<th scope="row"><label for="user_login"><?php _e(\'Username\'); ?> <span class="description"><?php _e(\'(required)\'); ?></span></label></th>
<td><input name="user_login" type="text" id="user_login" value="<?php echo esc_attr($new_user_login); ?>" aria-required="true" /></td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="email"><?php _e(\'E-mail\'); ?> <span class="description"><?php _e(\'(required)\'); ?></span></label></th>
<td><input name="email" type="text" id="email" value="<?php echo esc_attr($new_user_email); ?>" /></td>
</tr>
<tr class="form-field">
<th scope="row"><label for="first_name"><?php _e(\'First Name\') ?> </label></th>
<td><input name="first_name" type="text" id="first_name" value="<?php echo esc_attr($new_user_firstname); ?>" /></td>
</tr>
<tr class="form-field">
<th scope="row"><label for="last_name"><?php _e(\'Last Name\') ?> </label></th>
<td><input name="last_name" type="text" id="last_name" value="<?php echo esc_attr($new_user_lastname); ?>" /></td>
</tr>
<tr class="form-field">
<th scope="row"><label for="url"><?php _e(\'Website\') ?></label></th>
<td><input name="url" type="text" id="url" class="code" value="<?php echo esc_attr($new_user_uri); ?>" /></td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="pass1"><?php _e(\'Password\'); ?> <span class="description"><?php /* translators: password input field */_e(\'(required)\'); ?></span></label></th>
<td>
<input class="hidden" value=" " /><!-- #24364 workaround -->
<input name="pass1" type="password" id="pass1" autocomplete="off" />
</td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="pass2"><?php _e(\'Repeat Password\'); ?> <span class="description"><?php /* translators: password input field */_e(\'(required)\'); ?></span></label></th>
<td>
<input name="pass2" type="password" id="pass2" autocomplete="off" />
<br />
<div id="pass-strength-result"><?php _e(\'Strength indicator\'); ?></div>
<p class="description indicator-hint"><?php _e(\'Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ & ).\'); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row"><label for="role"><?php _e(\'Role\'); ?></label></th>
<td><select name="role" id="role">
<?php
$editable_roles = array_reverse( get_editable_roles() );
foreach ( $editable_roles as $role => $details ) {
if( \'administrator\' == $role )
continue;
$name = translate_user_role($details[\'name\'] );
if ( $selected == $role ) // preselect specified role
$p = "\\n\\t<option selected=\'selected\' value=\'" . esc_attr($role) . "\'>$name</option>";
else
$r .= "\\n\\t<option value=\'" . esc_attr($role) . "\'>$name</option>";
}
echo $p . $r;
?>
</select>
</td>
</tr>
</table>
<?php submit_button( __( \'Add New User \'), \'primary\', \'createuser\', true, array( \'id\' => \'createusersub\' ) ); ?>
</form>
<?php
}
最后要做的事情是处理提交请求,执行错误检查,然后使用
wp_insert_user()if($_POST[\'createuser\']){
//nonce verification for security
check_admin_referer(\'add-user\', \'_wpnonce_add-user\');
$user_login = $_POST[\'user_login\'];
$email = $_POST[\'email\'];
$first_name = $_POST[\'first_name\'];
$last_name = $_POST[\'last_name\'];
$pass1 = $_POST[\'pass1\'];
$pass2 = $_POST[\'pass2\'];
$url = $_POST[\'url\'];
$send_password = $_POST[\'send_password\'];
$role = $_POST[\'role\'];
//Handling errors
if( empty($user_login) || empty($email) ){
echo "Username or Email can\'t be empty";
}elseif( !is_email($email) ){
echo "Invalid email";
}elseif( empty($pass1) ){
echo "Password can\'t be empty";
}elseif( $pass1 !== $pass2 ){
echo "Password doesn\'t match";
}else{
//No errors. Create User.
$userdata = array(
\'user_login\' => $user_login,
\'user_pass\' => $pass1,
\'user_email\' => $email,
\'user_url\' => $url,
\'first_name\' => $first_name,
\'last_name\' => $last_name,
\'role\' => $role
);
$user_id = wp_insert_user( $userdata );
if( !is_wp_error($user_id) ){
//User created.
echo "User created";
}else{
//There was some error like duplicate email, username etc. Show the error
foreach( $user_id->errors as $name=>$errors ){
foreach( $errors as $error ){
echo $error;
}
}
}
}
}
那么你所要做的就是初始化这个类
$new_user = new create_user();
整个代码发布在下面
<?php
/*
Plugin Name: Create User
Description: Create user
Plugin URI: ****
Author: sakibmoon
Author URI: ****
Version: 0.0.1
*/
class create_user{
public function __construct(){
//Action hook to add menu option in the admin panel
add_action(\'admin_menu\', array( &$this, \'add_menu\') );
}
public function add_menu(){
//Creating top level menu as staff
//Use add_submenu_page for adding additional submenu
add_menu_page( \'Staff\', \'staff\', \'edit_users\', __FILE__, array( &$this, \'show_staff_page\' ) );
}
public function show_staff_page(){
//Processing submitted data
if($_POST[\'createuser\']){
//nonce verification for security
check_admin_referer(\'add-user\', \'_wpnonce_add-user\');
$user_login = $_POST[\'user_login\'];
$email = $_POST[\'email\'];
$first_name = $_POST[\'first_name\'];
$last_name = $_POST[\'last_name\'];
$pass1 = $_POST[\'pass1\'];
$pass2 = $_POST[\'pass2\'];
$url = $_POST[\'url\'];
$send_password = $_POST[\'send_password\'];
$role = $_POST[\'role\'];
//Handling errors
if( empty($user_login) || empty($email) ){
echo "Username or Email can\'t be empty";
}elseif( !is_email($email) ){
echo "Invalid email";
}elseif( empty($pass1) ){
echo "Password can\'t be empty";
}elseif( $pass1 !== $pass2 ){
echo "Password doesn\'t match";
}else{
//No errors. Create User.
$userdata = array(
\'user_login\' => $user_login,
\'user_pass\' => $pass1,
\'user_email\' => $email,
\'user_url\' => $url,
\'first_name\' => $first_name,
\'last_name\' => $last_name,
\'role\' => $role
);
$user_id = wp_insert_user( $userdata );
if( !is_wp_error($user_id) ){
//User created.
echo "User created";
}else{
//There was some error like duplicate email, username etc. Show the error
foreach( $user_id->errors as $name=>$errors ){
foreach( $errors as $error ){
echo $error;
}
}
}
}
}
?>
<?php //Input to create new User ?>
<form action="" method="post" name="adduser" id="adduser" class="validate"<?php do_action(\'user_new_form_tag\');?>>
<input name="action" type="hidden" value="adduser" />
<?php wp_nonce_field( \'add-user\', \'_wpnonce_add-user\' ) ?>
<?php
// Load up the passed data, else set to a default.
foreach ( array( \'user_login\' => \'login\', \'first_name\' => \'firstname\', \'last_name\' => \'lastname\',
\'email\' => \'email\', \'url\' => \'uri\', \'role\' => \'role\', \'send_password\' => \'send_password\', \'noconfirmation\' => \'ignore_pass\' ) as $post_field => $var ) {
$var = "new_user_$var";
if( isset( $_POST[\'createuser\'] ) ) {
if ( ! isset($$var) )
$$var = isset( $_POST[$post_field] ) ? wp_unslash( $_POST[$post_field] ) : \'\';
} else {
$$var = false;
}
}
?>
<table class="form-table">
<tr class="form-field form-required">
<th scope="row"><label for="user_login"><?php _e(\'Username\'); ?> <span class="description"><?php _e(\'(required)\'); ?></span></label></th>
<td><input name="user_login" type="text" id="user_login" value="<?php echo esc_attr($new_user_login); ?>" aria-required="true" /></td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="email"><?php _e(\'E-mail\'); ?> <span class="description"><?php _e(\'(required)\'); ?></span></label></th>
<td><input name="email" type="text" id="email" value="<?php echo esc_attr($new_user_email); ?>" /></td>
</tr>
<tr class="form-field">
<th scope="row"><label for="first_name"><?php _e(\'First Name\') ?> </label></th>
<td><input name="first_name" type="text" id="first_name" value="<?php echo esc_attr($new_user_firstname); ?>" /></td>
</tr>
<tr class="form-field">
<th scope="row"><label for="last_name"><?php _e(\'Last Name\') ?> </label></th>
<td><input name="last_name" type="text" id="last_name" value="<?php echo esc_attr($new_user_lastname); ?>" /></td>
</tr>
<tr class="form-field">
<th scope="row"><label for="url"><?php _e(\'Website\') ?></label></th>
<td><input name="url" type="text" id="url" class="code" value="<?php echo esc_attr($new_user_uri); ?>" /></td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="pass1"><?php _e(\'Password\'); ?> <span class="description"><?php /* translators: password input field */_e(\'(required)\'); ?></span></label></th>
<td>
<input class="hidden" value=" " /><!-- #24364 workaround -->
<input name="pass1" type="password" id="pass1" autocomplete="off" />
</td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="pass2"><?php _e(\'Repeat Password\'); ?> <span class="description"><?php /* translators: password input field */_e(\'(required)\'); ?></span></label></th>
<td>
<input name="pass2" type="password" id="pass2" autocomplete="off" />
<br />
<div id="pass-strength-result"><?php _e(\'Strength indicator\'); ?></div>
<p class="description indicator-hint"><?php _e(\'Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ & ).\'); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row"><label for="role"><?php _e(\'Role\'); ?></label></th>
<td><select name="role" id="role">
<?php
$editable_roles = array_reverse( get_editable_roles() );
foreach ( $editable_roles as $role => $details ) {
if( \'administrator\' == $role )
continue;
$name = translate_user_role($details[\'name\'] );
if ( $selected == $role ) // preselect specified role
$p = "\\n\\t<option selected=\'selected\' value=\'" . esc_attr($role) . "\'>$name</option>";
else
$r .= "\\n\\t<option value=\'" . esc_attr($role) . "\'>$name</option>";
}
echo $p . $r;
?>
</select>
</td>
</tr>
</table>
<?php submit_button( __( \'Add New User \'), \'primary\', \'createuser\', true, array( \'id\' => \'createusersub\' ) ); ?>
</form>
<?php
}
}
$new_user = new create_user();
EDIT: 我不完全明白你想做什么。但您似乎想要创建用户组。您要添加的人员是用户列表。它们也将显示在所有用户列表中。默认情况下,Wordpress没有类似用户组的内容。首先,您需要明确您试图用代码实现什么。