i know about the way to show/hide menu items in admin based on user roles, but anyone can just type the address and access the specific menu. I was wondering if there is a way to restrict that as well.
For the moment i'm using this code:
add_action( 'admin_init', 'my_remove_menu_pages' );
function my_remove_menu_pages() {
// If the user does not have access to publish posts
if(!current_user_can('add_users')) {
// Remove the "Tools" menu
remove_menu_page('tools.php');
}
}
How about going the other way:
http://codex.wordpress.org/Function_Reference/add_role
Related
Ok, so I found this code, which I modified to suit my needs. Btw, I'm using WooCommerce, which explains the "wc" in some of the function calls:
//Add login/logout link to primary menu
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li>Log Out</li>';
}
elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li>Log In</li>';
}
return $items;
This adds the login/logout menu items, and they work fine. However, they're stuck at the end of the menu, at the moment. I'd like to be able to edit the position using the editor in wp-admin. The solution I thought of was to maybe just create login and logout pages, and use header location redirects with those lines of code in them to get to the proper URLs, but the issue I see with that is that there will always be a login item and logout, no matter what status the user is currently in. Would there maybe be a way to dynamically add a site-wide CSS rule to hide the opposite menu item, based on the log in status?
Or is there an easier way?
Not the best idea but you can try.
Create in wp-admin menu section new menu item like "Custom Link"
Log Out
http://www.example.com/account/customer-logout/
Log In
http://www.example.com/account/
And add a custom class to a WordPress menu item to manage visibility
For example, you will see "logged-in" class on the body of the page and hide "Log In" link or change it to "Account" with the same link.
I am working on a project in wordpress.
In user options, when I select all users it shows a list of all users with edit / delete option. In my project, I want to remove delete option.
What do I need to do for this
Here is a snippet that hides the delete option from user with specific role (administrator in this case)
$role = get_role( 'administrator' ); // This is the user role
$role->remove_cap( 'delete_users' ); // This is the capability you remove
Refer to the codex for more info. Here are the two functions you need to use:
remove_cap()
get_role()
roles and capabilities - https://codex.wordpress.org/Roles_and_Capabilities#delete_users
You could use CSS to hide the delete option from appearing on hover. The following code adds custom styles the Admin area.
add_action('admin_head', 'hide_user_delete_option');
function hide_user_delete_option() {
echo '<style>
.users-php tr:hover .row-actions .delete{
visibility: hidden;
}
</style>';
}
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
I want that in wp-admin will be only Posts, Pages and Settings, it's possible to remove al remaining Media, Plugins, Users, Tools etc.
This function remove only from dashboard remove_menu_page( 'upload.php' );
Remove that menus from $restricted, that you want to prevent.
function remove_menus () {
global $menu;
$restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}
}
Credit goes to hungred via wprecipes
Since WordPress 3.1 you can better use remove_menu_page()
add_action( 'admin_menu', 'prefix_remove_menu_pages' );
function prefix_remove_menu_pages() {
remove_menu_page('edit-comments.php');
remove_menu_page('upload.php');
remove_menu_page('tools.php');
// Remove any item you want
}
}
from the docs:
Please be aware that remove_menu_pages would not prevent a user from accessing
these screens directly. Removing a menu does not replace the need to
filter a user's permissions as appropriate.
And for submenu items:
To remove submenu items in the admin, use remove_submenu_page. Using
remove_menu_page() will not work for submenu items.
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.