{"id":395,"date":"2011-11-25T00:55:45","date_gmt":"2011-11-24T16:55:45","guid":{"rendered":"http:\/\/octalforty.com\/?p=395"},"modified":"2023-12-08T11:23:01","modified_gmt":"2023-12-08T03:23:01","slug":"assigning-role-on-wordpress-registration-profile-page","status":"publish","type":"post","link":"https:\/\/octalforty.com\/articles\/assigning-role-on-wordpress-registration-profile-page\/","title":{"rendered":"Assigning Role On WordPress Registration & Profile Page"},"content":{"rendered":"
Phew. I didn’t expect that the previous article, Assigning Role On WordPress Registration Page<\/a> gained so much attention. This might be a little late for a follow up, but better late than never eh?<\/p>\n For this post, we’ll try to assign role on WordPress registration page, and then allow user to change their own role through their profile page. This might sound a bit risky<\/em> as we’re giving the user rights to change their own role, but we’ll limit the role choices to our custom roles only.<\/p>\n The changes should satisfy one of the scenarios<\/a> mentioned by Devexus. Oh, and we’ll do all this without depending on any plugins.<\/p>\n I’m sure by now, you should already familiarized yourself with Remember that the roles will allow certain capabilities to your user. If you’re using your roles as a way for the user to subscribe to a newsletter (or other uses) like Devexus, you might want to set the roles to be similar to the capabilities of Subscriber<\/a>.<\/p>\n Now that we have our roles added, we’ll need to include these new roles to the registration page. Simply include the code snippets below:<\/p>\n Save the file, and take a peek at your WordPress registration page. You should see a screen similar to the one below:<\/p>\nCreate the custom roles<\/h3>\n
functions.php<\/code>, so let’s open it and include the snippets below:<\/p>\n
<?php\n\/\/ Add two new role.\n\/\/ Full list of capabilities can be found at http:\/\/codex.wordpress.org\/Roles_and_Capabilities#Capability_vs._Role_Table\n add_role('writer', 'Writer', array( \n 'delete_posts' => true,\n 'delete_published_posts' => true,\n 'edit_posts' => true,\n 'edit_published_posts' => true,\n 'publish_posts' => true,\n 'read' => true,\n 'upload_files' => true,\n 'edit_users' => true\n));\n add_role('designer', 'Designer', array(\n 'edit_files' => true,\n 'edit_plugins' => true,\n 'edit_theme_options' => true,\n 'edit_themes' => true,\n 'install_plugins' => true,\n 'install_themes' => true,\n 'switch_themes' => true,\n 'update_plugins' => true,\n 'update_themes' => true,\n 'read' => true,\n 'edit_users' => true\n));<\/code><\/pre>\n<\/div>\n
Modifying WordPress Registration Page<\/h3>\n
add_action('register_form','role_registration_form');\nfunction role_registration_form(){\n $wp_roles = new WP_Roles();\n $wp_roles->use_db = true;\n $role_names = $wp_roles->get_names();\n \n foreach( $role_names as $role_name ) {\n \/\/ Ensure that the options exclude default Wordpress roles\n if ( ($role_name !== 'Administrator') and ($role_name !== 'Editor') and ($role_name !== 'Author') and ($role_name !== 'Contributor' ) and ($role_name !== 'Subscriber') ) {\n \/\/ Role value below needs to be in lowercase only\n $role_option .= \"<option value='\".strtolower($role_name).\"'>\";\n $role_option .= $role_name;\n $role_option .= \"<\/option>\";\n }\n }\n $html = '\n <style type=\"text\/css\">\n #role {\n background:#fbfbfb;\n border:1px solid #e5e5e5;\n font-size:24px;\n margin-bottom:16px;\n margin-right:6px;\n margin-top:2px;\n padding:3px;\n width:97%;\n }\n <\/style>\n\n <div width=\"100%\">\n <p>\n <label style=\"display: block; margin-bottom: 5px;\">' . __('Role', 'Role') . '\n <select id=\"role\" name=\"role\" class=\"input\">\n ' . $role_option . '\n <\/select>\n <\/label>\n <\/p>\n <\/div>\n ';\n echo $html;\n}<\/code><\/pre>\n<\/div>\n