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.
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.
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:At first we have to create the Admin menu page. We are going to add that in the constructor.
The
add_menu()
function is as followshow_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 theuser-new.php
with the exception of role. By default, wordpress usewp_dropdown_roles()
to populate the drop down field. But if you wanted to excludeAdministrator
you can’t call it directly. You have to removeAdministrator
fromeditable_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.The last thing to do is to handle the submit request, do error checking and then create user using wp_insert_user()
Then all you have to do is to initialize the class
The whole code is posted below
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.