Modify User Roles in WordPress? - wordpress

I have been working on a client's WordPress Website and last day my client want to hide navigation menu and pages from author/contributor categories.
I have searched and tried some of the plugin but didn't get the exact thing. Please let me know what should i use to hide some pages from user and from navigation.
Only Admin can see all the pages and other members should see only 1 section that is allowed to visible for them.
Thank You

use this plugin to manage All roll:
http://wordpress.org/plugins/user-role-editor/

Here is the Complete function for removing each Menu and submenu from wp-admin for another user:
function remove_menus() {
global $menu, $submenu;
$restricted = array(__('Dashboard'), __('Profile'), __('Users'), __('Tools'), __('Comments'), __('Settings'), __('Plugins')); //Here you can also define the name like Pages
end($menu);
while (prev($menu)) {
$value = explode(' ', $menu[key($menu)][0]);
if (in_array($value[0] != NULL ? $value[0] : "", $restricted)) {
unset($menu[key($menu)]);
}
}
unset($menu[5]); // this is just for example
unset($submenu['edit.php'][16]); // this is just for example
}
Now You have to put a conditon for other user i.e:
$thisusername = $current_user->user_login; // this is to get the current user login
if (($thisusername == "user123")) {
add_action('admin_menu', 'remove_menus');
}
Note: You can find many plugins but all of them are not in depth like this code.Well you can try this plugin to manage your user's roles.Capability Manager Plugin

Related

Hide plugin from admin bar & plugin list

I am trying to hide the following item from the following sections:
Admin bar: ID = wp-admin-bar-nitropack-top-menu
Plugin list: data-slug="nitropack"
I have tried these methods, but can not get it to work. Maybe i have the wrong IDs/Slugs?
Methods: https://divi.space/wordpress-and-divi-code-snippets/hide-any-plugin-from-the-wordpress-dashboard/
Would really appreciate some help, since a customer should not be able to change the settings within this plugin!
Best regards,
Isac
The css way
<style>
a[data-slug="nitropack"] { //hides all a href's with that data slug
display:none;
}
</style>
normally if its an wp admin menu you would do something like this:
//remove admin page item
function edit_admin_menus() {
remove_menu_page("index.php"); //Dashboard
remove_menu_page("itsec"); // wp-admin.php?page=itsec use this "itsec"
}
add_action("admin_menu", "edit_admin_menus");
or you need to remove admin bar item
//remove tool bar item
function remove_toolbar_node($wp_admin_bar) {
// replace 'updraft_admin_node' with your node id "nitropack" something
$wp_admin_bar->remove_node("avia");
$wp_admin_bar->remove_node("updates");
$wp_admin_bar->remove_menu("wp-logo");
$wp_admin_bar->remove_menu("themes");
$wp_admin_bar->remove_menu("widgets");
$wp_admin_bar->remove_menu("dashboard");
//$wp_admin_bar->remove_node("updraft_admin_node");
}
add_action("admin_bar_menu", "remove_toolbar_node", 999);
FYI, since you need to block access to the plugin you'll need to add a redirect based on member role. The customer may know the actual url and can still access the page.
//Admin or Editor role check, if else send to alt url
function block_pages_for_user() {
$blocked_pages = is_page('slug');
$url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if( !current_user_can('administrator') && !current_user_can('editor') && !current_user_can('subscriber') && $blocked_pages ) {
wp_redirect( 'http://www.example.dev/your-page/', 301 );
exit;
}
}
add_action( 'wp', 'block_pages_for_user', 8 );

Give access to only two (/home, /inbox) page for a particular user with specific role in wordpress

I want to give only two page (/home, /inbox) access to my user with role "Vendor", if user tries to access other pages than it will automatically redirect to "/inbox" page, I put the below code to achieve the functionality but after adding this to function.php, site again n again redirect and finally dies with message "Page isn't properly redirected". please suggest what is wrong with my tried code or any other solution.
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->slug,array("home","inbox"))) {
wp_safe_redirect('/inbox');
}
}
add_action('template_redirect', 'vendor_redirect');
The main issue the way I tried to get the page slug, the correct way to get the slug is "$post->post_name". also I put exit after wp_safe_redirect as well because codex suggest that:
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->post_name,array("home","inbox"))) {
wp_safe_redirect(get_permalink(get_page_by_path( 'inbox' )));
exit;
}
}
add_action('template_redirect', 'vendor_redirect');

Only logged in users should be able to view product in WooCommerce

I want to restrict WooCommerce products, shop page and category pages to logged in users only.
I do not want to achieve it with any plugin.
Please let me know if anybody done it before with any hook/filter/action.
Or I have to make WooCommerce template pages and add condition over there.
If I was to do this I would hook into the init action that WordPress offers.
Then do something like this in your theme's functions.php file:
function woo_check_logged_in()
{
if ( (is_product() || is_shop() ) && is_user_logged_in() )
{
}
else
{
die("You must be logged in to view this page");
}
}
add_action('init', 'woo_check_logged_in');
I haven't tested this but I believe it should get you on the right path without having to use any plugins.

Wordpress : Page that can be viewed only by members and if not a member than show them a registration page

I am trying to create a sampling process , where a user need to register or login to view my sample pdf documents , and once they register the user should be given a role as a sampler .
I am not a pro at wordpress but am learning . Can u guys suggest me some plugin or ideas on how to do it , i am using wordpress WordPress 3.3.1 .Thanks
http://codex.wordpress.org/Function_Reference/is_user_logged_in
http://codex.wordpress.org/Function_Reference/wp_login_form
As per your latest question, you will need to use these in tandem with each other, and make sure to define your redirect within your argument array to make sure the user is directed back to the page from which they logged in.
FINAL UPDATE: Enter this in your functions.php file and utilize as you see fit. This will allow you to output information according to the actual role of your user. This is also assuming that your plugin is creating actual custom roles. If not, you will have to utilize the Add Role Function:
function get_user_role()
{
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
Then in the template of the page you want:
<?php
if(is_user_logged_in())
{
switch(get_user_role())
{
case 'customer' :
//CUSTOMER DISPLAY
break;
case 'subscriber' :
//SUBSCRIBER DISPLAY
break;
default :
//OTHER TYPE OF USER DISPLAY
}
}
else
{
//NO LOGGED USER DISPLAY
}
?>

remove "profile" admin-menu from administrative panel

I am using WordPress, and I want to remove "profile" menu-option completely
Any one is having idea how can I achieve this?
Thanks
For the sake of completeness, here's how to do it programmatically...
// Run the function on admin_init
add_action('admin_init', 'remove_profile_menu');
// Removal function
function remove_profile_menu() {
global $wp_roles;
// Remove the menu. Syntax is `remove_submenu_page($menu_slug, $submenu_slug)`
remove_submenu_page('users.php', 'profile.php');
/* Remove the capability altogether. Syntax is `remove_cap($role, $capability)`
* 'Read' is the only capability subscriber has by default, and allows access
* to the Dashboard and Profile page. You can also remove from a specific user
* like this:
* $user = new WP_User(null, $username);
* $user->remove_cap($capability);
*/
$wp_roles->remove_cap('subscriber', 'read');
}
I know this is late but I just stumbled on this and thought I would add to it. That does remove the sub-menu profile menu item but does not remove the menu profile item. For someone like me who has created a completely custom profile page, I don't want my users to access the profile.php page at all. So this code will work for that:
function remove_profile_menu() {
remove_submenu_page('users.php', 'profile.php');
remove_menu_page('profile.php');
}
add_action('admin_menu', 'remove_profile_menu');
And if you only want to do this for certain capabilities....use this code:
function remove_profile_menu() {
// Only the Admin can see the profile menu
if(!current_user_can('update_core')) {
remove_submenu_page('users.php', 'profile.php');
remove_menu_page('profile.php');
}
}
add_action('admin_menu', 'remove_profile_menu');
You can use the current_user_can() function to determine who you want to see the menu items.
Profiless plugin does that on the subscriber-level.
If you wish to do that for other groups, you should probably use it in combination with Capability manager plugin.

Resources