I am trying to remove two links from users dashboard keeping it on admin but it goes off on both user and admin.
One i need to remove from user dashboard and other is contact form plugin link contact.
I am trying t o use below code.also post link goes off.
add_filter( 'admin_menu', 'remove_menus', 99 );
if (!current_user_can('manage_options')) {
add_action('wp_dashboard_setup', 'remove_menus' );
}
function remove_menus(){
remove_menu_page( 'index.php' ); //dashboard
}
you should follow the below code and also see the wordpress docs
<?php
function remove_menus(){
remove_menu_page( 'index.php' ); //Dashboard
//remove_menu_page( 'jetpack' ); //Jetpack*
//remove_menu_page( 'edit.php' ); //Posts
//remove_menu_page( 'upload.php' ); //Media
//remove_menu_page( 'edit.php?post_type=page' ); //Pages
//remove_menu_page( 'edit-comments.php' ); //Comments
//remove_menu_page( 'themes.php' ); //Appearance
//remove_menu_page( 'plugins.php' ); //Plugins
//remove_menu_page( 'users.php' ); //Users
//remove_menu_page( 'tools.php' ); //Tools
//remove_menu_page( 'options-general.php' ); //Settings
}
add_action( 'admin_menu', 'remove_menus' );
?>
wordpress official docs :-
page restriction at admin site for particular user redirect when user reached there.
add_action( 'current_screen', 'restrict_screen' );
function restrict_screen() {
if ( is_admin() ) {}
else{
$current_screen = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$indexpage = 'index.php';
$editpage = 'edit.php';
$match = strpos( $current_screen, $editpage );
$match2 = strpos( $current_screen, $indexpage );
if( $match == TRUE || $match2 == TRUE ) {
// wp_die('get out');
$current_admin = get_admin_url() . 'profile.php';
header('Location: ' . $current_admin . '', true, 301);
}
}
}
- on which role do you want show dashboard. please enter list of role like admin,subscriber etc. – user147874 Dec 28, 2018 at 11:51
- Want to add post to user dashboard using this `add_action( ‘admin_menu’, ‘remove_menus’ ); function remove_menus(){ if(!current_user_can(‘subscriber’)) add_menu_page( ‘edit.php’ ); //dashboard } as by default edit.php is not showing up…which enable users to post using their account – Ferna Dec 28, 2018 at 11:54
- okay you want only subscriber can’t access edit.php and other can access edit.php – user147874 Dec 28, 2018 at 12:00
- subscribe can access edit page so as to they register and start posting from their registered account – Ferna Dec 28, 2018 at 12:02
- then you should change role of subscriber to author or other role. its good think to do this. if subscriber can edit and post data then what is the difference between other roles dude can you understand what i’m trying to say. – user147874 Dec 28, 2018 at 12:04
- my complete code is add_filter( ‘admin_menu’, ‘remove_menus’, 99 ); function remove_menus(){ if(!current_user_can(‘administrator’)) remove_menu_page( ‘index.php’ ); //dashboard remove_menu_page( ‘wpcf7’ ); remove_menu_page( ‘upload.php’ ); //Media remove_menu_page( ‘edit-comments.php’ ); //Comments remove_menu_page( ‘tools.php’ ); //Tools } – Ferna Dec 28, 2018 at 12:23
- this gets remove from administrater even i choose as author – Ferna Dec 28, 2018 at 12:24
- try this if(!current_user_can(‘activate_plugins’)) – user147874 Dec 28, 2018 at 12:40
- plugins……checked no – Ferna Dec 28, 2018 at 12:44
- try your selft from codex.wordpress.org/Roles_and_Capabilities – user147874 Dec 28, 2018 at 12:45
- see my new edited answer – user147874 Dec 28, 2018 at 13:09
- got it need to put all links in array adding end ($menu); while (prev($menu)){ $value = explode(‘ ‘,$menu[key($menu)][0]); if(in_array($value[0] != NULL?$value[0]:”” , $restricted)){unset($menu[key($menu)]);} solved – Ferna Dec 28, 2018 at 13:11
- your welcome and please next do R & D on topic and go through the wordpress official documentation. – user147874 Dec 29, 2018 at 6:32
You can try this 🙂
add_filter( 'admin_menu', 'remove_menus', 99 );
function remove_menus(){
if(!current_user_can('administrator'))
remove_menu_page( 'index.php' ); //dashboard
}
Thats works but my contact form 7 link displays and post page link disappears
Admin menus and plugin menus are two different. You need to use separate actions to hide admin menus and plugin menus from admin dashboard. Here is how.
// Hide Admin Menus
function remove_menus() {
remove_menu_page( 'plugins.php' );
remove_menu_page( 'users.php' );
remove_menu_page( 'options-general.php' );
}
add_action( 'admin_menu', 'remove_menus' );
// Hide plugin menus
function remove_plugin_menu_pages() {
remove_menu_page( 'edit.php?post_type=acf' );
remove_menu_page( 'wpcf7' );
remove_menu_page( 'itsec' );
remove_menu_page( 'cptui_main_menu' );
remove_menu_page( 'revslider' );
}
add_action( 'admin_init', 'remove_plugin_menu_pages' );
- Why don’t you create a
editor
user role. I think you can avoid theseaction hooks
and achieve what you want. As I understood. – user2584538 Dec 28, 2018 at 11:18
Here’s what worked for me. This will only show Menu Items to Admin role & hide for other role(s). If you want to hide for Admin also, then remove the if statement.
// Hide Admin Menus from WP-Dashboard
add_filter( 'admin_menu', 'remove_menus');
function remove_menus(){
if(!current_user_can('administrator')) {
remove_menu_page( 'edit.php?post_type=page' ); //Contact Form7
remove_menu_page( 'edit.php?post_type=portfolio_item' ); //CPT
remove_menu_page( 'wpcf7' ); //Contact Form7
remove_menu_page( 'tools.php' ); //Tools
remove_menu_page( 'vc-welcome' ); //WPBakery Page Builder
}
}
source : https://wordpress.stackexchange.com/questions/324043/remove-dashboard-links-from-wordpress