Wordpress remove menu items from wp-admin - wordpress

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.

Related

WordPress add Login/Logout to menu editor

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.

Hide specific dashboard items for user in Wordpress

In my website dashboard i have to hide Media, Comments, Contact menu items for one user who has administrator role. I googled that I have to use remove_cap function, but couldn't find enough information even to start my task.
You can put something like this inside your functions.php. :
add_action( 'admin_menu', 'adjust_the_wp_menu', 999999 );
function adjust_the_wp_menu() {
$user = wp_get_current_user();
if($user && isset($user->user_login) && 'desired_user_login' == $user->user_login)
{
//Remove menu items from admin area
remove_menu_page( 'edit-comments.php' );//For comments
remove_menu_page( 'upload.php' );//For media
//Remove third party plugins admin menu items
remove_menu_page( 'edit.php?post_type=your_custom_post_slug' );//for custom posts
remove_menu_page( 'your_plugin_page_slug' );//for custom plugin's page
// do stuff
}
}
Not sure what is the Contact menu in your Wordpress. I guess it is added by some plugin. You can try to figure out it's slug & use it inside the code above.
Also note, that it will only hide the menu items. Pages itself will be still available if respective url is accessed directly. To prevent this you should manage user roles or put some conditional logic into your functions.php.

Top-level menu item to edit a specific page

I'm not sure if this is possible or advisable, but I'd like to have a top-level menu item on the sidebar in the admin dashboard that links to a specific page within wordpress for editing.
Maybe there's a better way of doing this... here's the functionality I'm after:
I have a page called "Upgrade Contents" where my client can edit the contents of their upgrade package sitewide. I'd like them to be able to edit this page directly from the admin dashboard, like a setting page. Problem is, I don't know how to add a link to edit this page to the admin AND I already have everything set up with ACF using this page.
Is adding a link easy to do or should I just scrap it and make a settings page for my theme and add THAT to the admin?
To add a link at the top level of the menu you can add an item to the global $menu array, like this:
function link_to_user_settings() {
global $menu;
$title = 'User Settings';
$url = 'URL_OF_YOUR_PAGE';
$position = 73;
$permission = 'read';
$icon = 'dashicons-admin-links';
$menu[$position] = array( $title, $permission, $url, '', 'menu-top', '', $icon );
}
add_action( 'admin_menu', 'link_to_user_settings' );
Change the variables accordingly to your needs.
The $position var is where you want the link to appear based on the default positions you can find at the add_menu_page() documentation (in my example 73 means after the Users menu item).

Remove Option Tree menu in admin page

I integrated option tree in my template.
I want to hide OptionTree menu item from users. How to remove Option Tree menu item in admin page?
Add this code to your theme's functions.php:
// Remove Option Tree Settings Menu
add_filter( 'ot_show_pages', '__return_false' );
That will remove the Option Tree admin menu.
Here is another solution.
function remove_ot_menu () {
remove_menu_page( "ot-settings" ); } add_action( 'admin_menu', 'remove_ot_menu' );
I know i'm late to the party but since i got to find
a solution (for an old website i restructering) i tought
i might share "a softer" solution.
2 steps
1. We ad the current user id to the admin body class
2. We add a css to hide the menu except for the intended user.
User id in the body class
/*********************************************
** CUSTOM BODY CLASS
*********************************************/
add_filter('admin_body_class', 'custom_admin_body_class');
function custom_admin_body_class($classes){
$cuserid = get_current_user_id();
return $classes. 'user-'.$cuserid;
}
Add the desired css to anytype of css loaded to wp-admin
** replace user-[number] with yours*
.wp-admin:not(.user-1) #toplevel_page_ot-settings {display: none;}
If your not loading any css to wp-admin you can use this
add_action('admin_head', 'my_custom_fonts');
function my_custom_fonts() {
echo '<style>.wp-admin:not(.user-1) #toplevel_page_ot-settings {display: none;}</style>';
}

How do you add a WordPress admin page without adding it to the menu?

I'm building a WordPress plugin and I'd like to have an edit-item page that can't be reached via the submenu (because then the item wouldn't be specified).
This resource (http://codex.wordpress.org/Adding_Administration_Menus) shows how to associate an admin page with a function, but not how to do so without adding it as a menu item.
Can this be done?
Thanks!
Best solution here http://wordpress.org/support/topic/add-backend-page-without-menu-item
use add_submenu_page with parent slug = null
I have finally discovered a way to do this that isn't an ugly hack, doesn't require JS to highlight the desired menu item (and submenu item), and works for regular menus registered by plugins (#Josh's answer only works for custom post types).
Essentially, you just need to register your submenu normally, but then hook into the 'submenu_file' filter to deregister it and optionally also set another submenu item to highlight instead.
function so3902760_wp_admin_menu() {
// Register the parent menu.
add_menu_page(
__( 'Parent title', 'textdomain' )
, __( 'Parent', 'textdomain' )
, 'manage_options'
, 'my_parent_slug'
, 'display_my_menu'
);
// Register the hidden submenu.
add_submenu_page(
'my_parent_slug' // Use the parent slug as usual.
, __( 'Page title', 'textdomain' )
, ''
, 'manage_options'
, 'my_hidden_submenu'
, 'display_my_submenu'
);
}
add_action( 'admin_menu', 'so3902760_wp_admin_menu' );
function so3902760_wp_admin_submenu_filter( $submenu_file ) {
global $plugin_page;
$hidden_submenus = array(
'my_hidden_submenu' => true,
);
// Select another submenu item to highlight (optional).
if ( $plugin_page && isset( $hidden_submenus[ $plugin_page ] ) ) {
$submenu_file = 'submenu_to_highlight';
}
// Hide the submenu.
foreach ( $hidden_submenus as $submenu => $unused ) {
remove_submenu_page( 'my_parent_slug', $submenu );
}
return $submenu_file;
}
add_filter( 'submenu_file', 'so3902760_wp_admin_submenu_filter' );
Yes, this can be done (well, technically, it would be more like registering the whole thing and then removing the menu item later), but It would just be easiest (I think) to check for parameters in the $_GET super-global to indicate that the user wishes to edit a specific item.
For example, you could have a page that lists items to edit, and clicking 'edit' only adds the item's ID to the current URL(query-string).
In the function that displays this page, if ID is defined, give them the page to edit that item.
Otherwise, give them the list view. That's how posts, pages, and other custom post types do it.
add_submenu_page with parent slug = null
OR
add_submenu_page with menu title = null
use this code for creating new page without adding in menu
add_action( 'admin_menu', 'register_newpage' );
function register_newpage(){
add_menu_page($appname, $appname, 'administrator','custompage', 'custom');
remove_menu_page('custom');
}
function custom()
{
echo "hai";
}
Note: This solution doesn't automatically set the current menu and submenu item. If you want to highlight a particular menu as current when the hidden page is viewed, see my other answer.
From the answers that come before me, you can see that there are many ways to do this. However, there is another way that I think may be the best.
Loading the page differently based on the value of a $_GET query var is one option, but it may not be what some people are looking for.
The suggestions regarding add_submenu_page() are on the right track, but each of the previous suggestions have problems. Setting $menu_title to null doesn't keep the menu item from being displayed, it just makes it so the link doesn't have any text. The link still takes up some room in the menu though, so it looks funny. Setting the $parent_slug to null doesn't have this problem, but I noticed that the page's HTML title doesn't display the $page_title text.
My solution was to set $parent_slug to a fake menu slug, like 'i_dont_exist'. The menu item won't be displayed, and when viewing the admin screen the page title will be filled out properly.
add_submenu_page(
'_doesnt_exist'
,__( 'Page title', 'textdomain' )
,''
,'manage_options'
,'menu_slug'
,'display_my_menu'
);
Yes. It is very possible to make a page cannot be reach via submenu, or even the main menu in the WP admin panel. See the code snippet below.
function myplugin_render_edit_page() {
// Code contains the UI for edit page.
}
/**
* Manage menu items and pages.
*/
function myplugin_register_admin_page() {
global $_registered_pages;
$menu_slug = plugin_basename('myplugin.php');
$hookname = get_plugin_page_hookname($menu_slug,'');
if (!empty($hookname)) {
add_action($hookname, 'myplugin_render_edit_page');
}
$_registered_pages[$hookname] = true;
}
add_action('admin_menu', 'myplugin_register_admin_page');
Hopefully, this will help.
Create sub menu page and parent slug leave it empty like this:
// Create page were you can add new users.
public function add_create_user_menu() {
add_submenu_page(
'',
'Create User',
'Create User',
'manage_options',
'create-user',
array( $this, 'add_create_user_page' )
);
}
You can access it like this:
Add New
I've tried all of the suggestions here but with various issues associated with each.
The WordPress codex for add_submenu_page now gives the correct answer, which is to use options.php as your parent slug. I tried the trick of using a made up name but that gives permissions errors, equally use of null at various locations either causes the menu text to simply be missing (but still clickable) or for the browser title to go missing.
Using options.php worked and I've not seen any issues as a result of its use.
Using add_submenu_page with a parent of NULL definitely works, however if you want to keep the non-linked page associated with a particular menu (say a custom post type menu), you have to use a variation of #Boopathi's answer:
function my_hidden_submenu_page(){
//add the submenu page the usual way
add_submenu_page('edit.php?post_type=custom-type', 'My Page Title', 'My Page Title', 'manage_options', 'my-page-slug', 'my_page_callback');
//then remove it
remove_submenu_page('edit.php?post_type=custom-type','my-page-slug');
}
add_action('admin_menu', 'my_hidden_submenu_page');
It looks as though the two actions would cancel each other out, however remove_submenu_page does not unregister the callback function; it merely removes the link.
This way when someone is viewing your non-linked page, the correct navigation menu (our custom post type menu in this example) will still show as active.
One of the problems I found with merely adding null as the parent slug for a sub menu item is that if you're currently viewing that specific page the submenu itself won't display (at least it didn't for me (along with the page title not showing).
What I did was add an empty span element inside the menu title and use jquery to traverse the parent elements and hide it.
It seems this need is still valid for nowadays version.
I am using WordPress 5.3.2 and I used the following methods to remove the parent named menu from the submenu.
add_action( 'admin_menu', 'submenus' );
function submenus()
{
global $submenu;
$parent_slug = 'parent_slug_name';
// remove parent named menu from submenu because it is always the first one in the submenu array, so the offset is 0 and remove just 1
array_splice( $submenu[$parent_slug], 0, 1 ); // with reindex
// or unset( $submenu[$parent_slug][0] ); // without reindex
}
Hope it helps others who want to achieve the same goal.
About php array_splice()
this method could be found in the source of WP function remove_submenu_page() which is available since WP 3.1.0
Edit / Added
Apart from submenu, parent menu could also be updated in similar way.
For parent menu, the global variable is $menu.
example for reference:
add_action( 'admin_menu', array( $this, 'modify_menu_title' ) );
function modify_menu_title() {
global $menu;
$page = 'some-page-slug';
$new_menu_title = 'New Title Name';
foreach( $menu as $key => $value ) {
if( $menu[$key][2] === $page ) {
$menu[$key][0] = $new_menu_title;
}
}
}
I find you can do it by reusing the insert id, like so:
add_menu_page( 'User AS Packages', 'User AS', 'manage_options', 'myplugin/editaspackages.php', 'UserASPackages', '', 8);
add_menu_page( 'User ARP Packages', 'User ARP', 'manage_options', 'myplugin/editarppackages.php', 'UserARPPackages', '', 8);
add_menu_page( 'AS Packages', 'AS Packages', 'manage_options', 'myplugin/ars-s2.php', 'ARPPackages', '', 8);
The last 3 using position 8 and the last one overrides the two before so the two before do not appear.

Resources