Add Role to Registration in WP - wordpress

I am looking for a way to add a dropdown for users in the registration form to select their role (except administrator).
Currently running wordpress 4.7.4 with buddypress 2.8.0., found some snippets and such but none of them are actually working.
Any help is much appreciated

You have to create user-role.php fileand include it in functions.php file.
<?php
if( get_role('subscriber') ){
remove_role( 'subscriber' );
}
if( get_role('client') ){
remove_role( 'client' );
}
// Add a Country (Others) role
$result = add_role( 'country', __(
'Country (Others)' ),
array(
'read' => true, // true allows this capability
'edit_posts' => true, // Allows user to edit their own posts
'edit_pages' => true, // Allows user to edit pages
'edit_others_posts' => true, // Allows user to edit others posts not just their own
'create_posts' => true, // Allows user to create new posts
'manage_categories' => true, // Allows user to manage post categories
'publish_posts' => true, // Allows the user to publish, otherwise posts stays in draft mode
'edit_themes' => false, // false denies this capability. User can’t edit your theme
'install_plugins' => false, // User cant add new plugins
'update_plugin' => false, // User can’t update any plugins
'update_core' => false // user cant perform core updates
)
);

Related

tgm register required plugin activation hook not working

I am facing a problem with tgm i have just download the setup from tgm site
http://tgmpluginactivation.com/download/
and when i follow the steps as described on the site and in the downloaded zip file but after follow the steps when i activate my custom theme its simply activated its did not showing any message regarding activating the required plugins
my themes function.php file code is below
<?php
require_once get_template_directory() . '/includes/class-tgm-plugin-activation.php';
add_action( 'tgmpa_register', 'custom_register_required_plugins' );
function custom_register_required_plugins() {
/*
* Array of plugin arrays. Required keys are name and slug.
* If the source is NOT from the .org repo, then source is also required.
*/
$plugins = array(
// This is an example of how to include a plugin bundled with a theme.
array(
'name' => esc_html__('Visual Composer','custom'),
'slug' => 'js_composer',
'source' => get_template_directory() . '/plugins/js-composer.zip',
'required' => false,
'version' => '5.1.1',
'force_activation' => false,
'force_deactivation' => false,
'external_url' => '',
),
);
$config = array(
'id' => 'custom', // Unique ID for hashing notices for multiple instances of TGMPA.
'default_path' => '', // Default absolute path to bundled plugins.
'menu' => 'tgmpa-install-plugins', // Menu slug.
'has_notices' => true, // Show admin notices or not.
'dismissable' => true, // If false, a user cannot dismiss the nag message.
'dismiss_msg' => '', // If 'dismissable' is false, this message will be output at top of nag.
'is_automatic' => false, // Automatically activate plugins after installation or not.
'message' => '', // Message to output right before the plugins table.
);
tgmpa( $plugins, $config );
}
please tell me whats wrong in this code why after acitvate the theme did not show any message about activate the required plugins

WordPress: User with custom role cannot access wp-admin

First of all I am a WordPress learner. So sorry if my code looks stupid!
I have created a custom theme with a custom user role. I am not developing any plugin.
In my fucntions.php file I have written the following code to create a User role. Users assigned to this role are supposed to login to the admin but only be able to access their Profile pages.
add_action('init', 'yrc_cst_register_role_customer_service_rep');
/**
* Register new user role
*/
function yrc_cst_register_role_customer_service_rep() {
$wp_roles = new WP_Roles();
$wp_roles->remove_role('subscriber');
$wp_roles->remove_role('editor');
$wp_roles->remove_role('contributor');
$wp_roles->remove_role('author');
$service_rep_caps = array(
'read' => false,
'create_posts' => false,
'edit_posts' => false,
'edit_others_posts' => false,
'publish_posts' => false,
'manage_categories' => false,
'manage_options' => false,
);
add_role('customer_service', __('Customer Service'), $service_rep_caps);
}
I have removed all roles except Administrator, because no other role is required for this portal. Administrator will only create Users with Customer Service role.
I have no third party plugin installed in the system.
Users with the custom role are able to login to the system through a custom login page which is working OK. But whenever they are trying to access their Profile page the following error message comes up:
Sorry, you are not allowed to access this page.
Is there anything like 'edit_profile' => true?
I must be doing something wrong but my limited knowledge is not enough to figure this out. Any suggestion would be highly appreciated.
You might be able to do it like this :
This should clone the subscriber role capabilities and create your role for it.
add_action('init', 'CreatecloneRoleSubscriber');
function CreatecloneRoleSubscriber()
{
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$sub = $wp_roles->get_role('Subscriber');
//Adding a 'new_role' with all subscriber caps
$wp_roles->add_role('customer_service', 'Customer Service', $sub->capabilities);
}
EDIT : Read discussion in question comments
Just change the manage_options to true in your case
But note that by allowing manage_options to true, those user will have access to other parts of dashboard as well
$service_rep_caps = array(
'read' => false,
'create_posts' => false,
'edit_posts' => false,
'edit_others_posts' => false,
'publish_posts' => false,
'manage_categories' => false,
'manage_options' => true, // Most plugins and pages check for manage_options for checking access level to allow access to pages and settings.
);
Just found this solution which i consider really clean to enter wp-admin with a new role:
Add access cap to backend
Add the cap view_admin_dashboard & read to your new role.
Show the admin bar
Add this function to your wordpress.
# functions.php
add_filter( 'show_admin_bar', function () {
if ( current_user_can( 'view_admin_dashboard' ) )
return true;
return false;
}, 10);

Limit WordPress user to edit only his own pages

I'm looking for the simplest way to limit a WordPress user to edit only his own pages (that is pages he is author of). I've read about some users manager plugins but for my needs they seems overkill and so I wonder if it is possible to obtain the same result adding some code lines to functions.php or something similar.
you can do this by adding a new role like so :
<?php add_role( $role, $display_name, $capabilities ); ?>
This setting is saved to the database (in table wp_options, field wp_user_roles), so it might be better to run this on theme/plugin activation
Returns a WP_Role object on success, null if that role already exists.
Example
Create a new "Basic Contributor" role.
$result = add_role(
'basic_contributor',
__( 'Basic Contributor' ),
array(
'read' => true, // true allows this capability
'edit_posts' => true,
'delete_posts' => false, // Use false to explicitly deny
)
);
if ( null !== $result ) {
echo 'Yay! New role created!';
}
else {
echo 'Oh... the basic_contributor role already exists.';
}
add_role() is located in wp-includes/capabilities.php.
for more clarification look in this article

WordPress add roles capabilities edit user

I want to add the capability of adding/eddeting/removing users to a new role.
However I can't find the right capabilities.
Any help please?
Current (from the WordPress Codex)
add_role('new-role', 'New Role', array(
'read' => true, // True allows that capability
'edit_posts' => false,
'delete_posts' => false, // Use false to explicitly deny
));
I would like to add something like: add_user, edit_user and remove_user
M.
-EDIT--EDIT--EDIT--EDIT--EDIT--EDIT-
I have made some progress.
This is what I have now:
add_role('new-role', 'New Role', array(
'read' => true, // True allows that capability
'edit_posts' => false,
'delete_posts' => false, // Use false to explicitly deny
'edit_users'=> true,
'level_10'=> true,
'delete_users' => true,
'create_users' => true,
'list_users'=>true,
'remove_users' > true,
'add_users' => true,
'promote_users'=> true
));
I can now add users but I can't edit users. The "user" tab doesn't show.
There is a "add new user" button under the "profile" menu.
M.
-EDIT--EDIT--EDIT--EDIT--EDIT--EDIT-
Okay I fixed it.
I was allready using the 'Adminimize' plugin so I duplicated the "Administrator"-role and hide everything that I didn't need:
function cloneRole()
{
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$adm = $wp_roles->get_role('administrator');
//Adding a 'new_role' with all admin caps
$wp_roles->add_role('new_role', 'My Custom Role', $adm->capabilities);
}
Sadly, there is no ability within WordPress to set a role to be able to edit users. Reading through the WordPress source code, there is only the add and delete options for users but there are edit buttons for plugins, themes, posts, pages etc.
The actual code can be found here: WordPress Capabilities Trac
The lines it shows the user related roles are around line 1273 to 1288.
Sorry to disappoint.

Wordpress add custom roles as well as remove default roles

I need to customize the default roles as I need only 3 roles - administrator, buyer, seller.
Then I need to add the buyer, seller and remove all other default roles. What should I do?
If there is any ready made code which I can paste in and it will work?
Paste this code in your themes function.php file and customize as your need. This is from my own code library. So it will definitely work.
/* Add member role to the site */
add_role('member', 'Member', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
));
/* Add snypher role to the site */
add_role('snypher', 'Snypher', array(
'read' => true,
'edit_posts' => true,
'delete_posts' => true,
));
/* remove the unnecessary roles */
remove_role('subscriber');
remove_role('editor');
remove_role('author');
remove_role('contributor');

Resources