新用户表单-自定义菜单页面

时间:2014-01-04 作者:jameslcarton

我有一个关于创建新用户的问题。我正在为我正在开发的这个wp站点创建一个插件。我已经为这些角色设置了自定义角色,我想为“所有者”角色设置一个单独的菜单项,以便能够访问和添加新的工作人员。我会使用“添加用户”菜单,但我想对其进行自定义,以显示“员工”和“添加新员工”,但我不能这样做,我不这么认为。所以,我选择了这条路线。无论如何我曾尝试在“user new.php”和“users.php”的上下文中复制并更改路径,但我仍然会遇到错误。我尝试过复制查看源代码的内容,但也没有成功。我想要大致相同的设置。。。可能会从选项中排除“管理员”角色。如果您对此有任何建议,我们将不胜感激。提前向任何能提供帮助的人表示感谢!

EDIT:

我有一个包含的核心文件require_once \'includes/plugin_hooks.php\';然后在我的plugin\\u挂钩中。php我已经写好了:

//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\');
然后,在用户添加成功后,我希望它重定向到列出所有工作人员的人员页面。

1 个回复
SO网友:sakibmoon

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\') );
}
Theadd_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 ! " ? $ % ^ &amp; ).\'); ?></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 ! " ? $ % ^ &amp; ).\'); ?></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没有类似用户组的内容。首先,您需要明确您试图用代码实现什么。

结束

相关推荐

How can I find plugins' slug?

我想知道如何才能找到插件的slug(slug=WordPress用来更新插件和确定哪些插件当前处于活动状态的内部名称)?它通常是插件的文件夹名,但如果插件没有文件夹,则是其文件名(如hello.php)。是否有其他例外情况?大小写字符重要吗</插件的slug是否可以与其文件夹名不同?如果有一个叫做hello的插件呢。php和另一个/您好。php/您好。php