admin管理员组文章数量:1315258
I have a question concerning creating a new user. I'm creating a plugin for this wp site I am working on. I have set up custom roles for these and I'd like to have a separate menu item for the "Owner" role to be able to access and add new staff members. I'd use the add users menu but I'd like to customize it to say "Staff" and "Add New Staff" but I can't do that, I don't think. So, I've opted for this route. Anyway... I've tried copying over the context of the "user-new.php" and "users.php" and change paths but I continue to run into errors. I've tried copying over stuff viewing the source but that hasn't worked either. I'd like pretty much the same exact setup... maybe excluding "Administrator" role from the options. Any pointers on this would be greatly appreciated. Thank you in advance to anyone that can help!
EDIT:
I have my core file with an include require_once 'includes/plugin_hooks.php';
Then in my plugin_hooks.php I have this written out:
//Eleven34 Studios Staff Manager Menu
function e34s_staff_plugin_menu() {
add_menu_page('Staff','Staff','read','e34s-staff','e34s_staff_list',
'/wordpress/wp-content/plugins/e34s_time_card/includes/images/e34s_staff_icon.png',22);
}
function e34s_staff_list() {
include ('staff/staff.php');
}
add_action('admin_menu','e34s_staff_plugin_menu');
//Submenu items
function e34s_staff_plugin_add_staff() {
add_submenu_page( 'e34s-staff', 'Add Staff', 'Add Staff', 'read', 'e34s_staff_add_staff', 'e34s_staff_add_staff_callback' );
}
function e34s_staff_add_staff_callback() {
include ('staff/add_staff.php');
}
add_action('admin_menu', 'e34s_staff_plugin_add_staff');
Then after the user is added successfully I want it to redirect to the staff page that lists all of the staff members.
I have a question concerning creating a new user. I'm creating a plugin for this wp site I am working on. I have set up custom roles for these and I'd like to have a separate menu item for the "Owner" role to be able to access and add new staff members. I'd use the add users menu but I'd like to customize it to say "Staff" and "Add New Staff" but I can't do that, I don't think. So, I've opted for this route. Anyway... I've tried copying over the context of the "user-new.php" and "users.php" and change paths but I continue to run into errors. I've tried copying over stuff viewing the source but that hasn't worked either. I'd like pretty much the same exact setup... maybe excluding "Administrator" role from the options. Any pointers on this would be greatly appreciated. Thank you in advance to anyone that can help!
EDIT:
I have my core file with an include require_once 'includes/plugin_hooks.php';
Then in my plugin_hooks.php I have this written out:
//Eleven34 Studios Staff Manager Menu
function e34s_staff_plugin_menu() {
add_menu_page('Staff','Staff','read','e34s-staff','e34s_staff_list',
'/wordpress/wp-content/plugins/e34s_time_card/includes/images/e34s_staff_icon.png',22);
}
function e34s_staff_list() {
include ('staff/staff.php');
}
add_action('admin_menu','e34s_staff_plugin_menu');
//Submenu items
function e34s_staff_plugin_add_staff() {
add_submenu_page( 'e34s-staff', 'Add Staff', 'Add Staff', 'read', 'e34s_staff_add_staff', 'e34s_staff_add_staff_callback' );
}
function e34s_staff_add_staff_callback() {
include ('staff/add_staff.php');
}
add_action('admin_menu', 'e34s_staff_plugin_add_staff');
Then after the user is added successfully I want it to redirect to the staff page that lists all of the staff members.
Share Improve this question edited Jan 7, 2014 at 11:39 sakibmoon 2,2841 gold badge19 silver badges25 bronze badges asked Jan 4, 2014 at 4:36 jameslcartonjameslcarton 231 silver badge5 bronze badges1 Answer
Reset to default 1Important: 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.
I am going to use the class structure for the plugin and let's say plugin name is create-user
. So, the basic structure of this plugin will be:
<?php
/*
Plugin Name: Create User
Description: Create user
Plugin URI: ****
Author: sakibmoon
Author URI: ****
Version: 0.0.1
*/
class create_user{
}
At first we have to create the Admin menu page. We are going to add that in the constructor.
public function __construct(){
//Action hook to add menu option in the admin panel
add_action('admin_menu', array( &$this, 'add_menu') );
}
The add_menu()
function is as follow
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()
function will show the page content. We will show the form to create user as page content. I have taken almost all the portion from the user-new.php
with the exception of role. By default, wordpress use wp_dropdown_roles()
to populate the drop down field. But if you wanted to exclude Administrator
you can't call it directly. You have to remove Administrator
from editable_roles
. To save time, I have hard coded it. But in my opinion best choice would be to use the filter editable_roles. How to remove administrator role in settings -> general -> New User Default Role? will give you some idea about this.
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
}
The last thing to do is to handle the submit request, do error checking and then create user using 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;
}
}
}
}
}
Then all you have to do is to initialize the class
$new_user = new create_user();
The whole code is posted below
<?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: I don't fully understand what you are trying to do. But it seems that you want to create user group. The staff that you are adding is list of users. They will be shown in all user list too. Wordpress by default doesn't have anything like user group. First you need to clear what you are trying to achieve with your code.
本文标签: pluginsNew User FormCustom Menu Page
版权声明:本文标题:plugins - New User Form - Custom Menu Page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741980013a2408340.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论