Remove/ trim a number of links from drupal navigation - drupal

How would I trim/ unset a certain number of menu from drupal navigation menu to avoid lengthy menus for a specific page? Say I want to remove the last 20 menu items from navigation menu.
Any advice would be very much appreciated.
Thanks

You could do it at the theme layer easy enough by theming your menu.
See theme_links()
http://api.drupal.org/api/function/theme_links/6
Perhaps something like:
function mytheme_links($links, $attributes = array('class' => 'links')) {
$length = variable_get('mytheme_menu_length', 22);
$links = array_slice($links, 0, $length);
return theme_links($links, $attributes);
}
There are other ways also. You could hide some with CSS or build the $links array yourself but the menu api doesn't really have an interface for this.
See menu_navigation_links().
http://api.drupal.org/api/function/menu_navigation_links/6

Related

How do I iterate through Wordpress admin menu sub menus?

I can iterate through the top level Wordpress menu items to change the name but I now need to iterate through the sub menu items of that menu. I know I can do it using exact values $submenu['edit.php'][5][0] = 'New Label'; but it's for a plugin where the menu item may not be in the same position every time so that won't work.
I've got this code for the top level menus.
function _rename_top_level_menu( $original, $new ) {
global $menu;
foreach( $menu as $key => $value ) {
if( $original === $value[0]) {
$menu[$key][0] = $new;
}
}
}
Have you tried to use menu walker?
Although the Walker class has many uses, one of the most common usages by developers is outputting HTML for custom menus (usually ones that have been defined using the Appearance → Menus screen in the Administration Screens).

How to display language switcher as dropdown in Polylang?

I don't have display as dropdown in Appereance -> Menu -> Language switcher. How can i solve it?
Here is an image
Any help will be appreciated...
You should alternatively use the PHP function to display it, try this
<?php pll_the_languages( array( 'dropdown' => 1 ) ); ?>
The documentation is here - https://polylang.pro/doc/function-reference/#pll_the_languages
In appearance->menus, go in screen options on top right of you screen and check the “Language switcher” checkbox. You then should have a new metabox which allows to add a language switcher just as you add another menu item.
#NJENGAH's answer is correct.
Additionally, if you would like to have 2 drop-downs on the same page, i.e. header and footer, increment the dropdown array value for the subsequent calls by 1.
<?php pll_the_languages( ['dropdown' => 2 ] ); ?>```
and so on...

How do I add content types other than Posts in the WordPress admin area?

I want to add a completely custom content type to the WordPress admin panel as per my image below. I don't believe this is called a plugin as I did a tutorial on those and they don't have an admin interface. I want to define a custom create/edit/delete screen for this content.
Is this possible?
What should I be searching for to get help on this?
I believe what you're looking for (as of Wordpress 3.0) is custom post types. There's quite a good tutorial on them here, however googling "wordpress custom post types" should provide a plethora of links.
http://codex.wordpress.org/Function_Reference/add_submenu_page is a good starting place to look. It will explain how to add new items into the menu.
Now, removing them is a different story. This function I wrote for one of my plugins removes entries from a submenu:
function cleanup_menu() {
global $submenu, $wpdb;
$new_submenu = array();
$remove = array( 'Cast Manager', 'Seating Manager', 'Download Reports', 'new report' );
foreach ($submenu['menuname'] as &$item) {
if ( ! in_array( $item[0], $remove ) ) { $new_submenu[] = $item; }
}
$submenu['menuname'] = $new_submenu;
}
So in your case, "menuname" would be changed to "Posts", I would guess. There's also a function remove_submenu_page, but there's no documentation on it and I haven't looked into it.

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.

fetching only part of a Drupal 6 menu

I'd like to pull out just one section of my navigation menu - a single section of the admin menu structure. I can load the entire navigation menu tree, but I can't see an easy way of pulling out just one segment of it.
Is there an easy way to do this, or do I have to do something hacky?
Have a look at function menu_navigation_links. You pass it a menu name (default = navigation) and a level (default = 0).
Not sure if you'd consider this an easy way, but you could try to grab the whole menu tree via menu_tree_data() or menu_tree_page_data(), find and extract the section you're interested in from the resulting tree structure and render the resulting subtree via menu_tree_output().
EDIT: Stumbled over How to rendering a menu subtree in the meantime - looks like my suggestion could work, but I would definitely not consider this being easy ;)
I found this here which works great for me.
<?php
$menus = menu_tree_page_data(menu_get_active_menu_name()); //get menu tree for active menu
$output='';
foreach($menus as $data) {
if(!empty($data['link']['in_active_trail'])){
$link = theme('menu_item_link', $data['link']);
$extra_class = NULL;
if ($data['below']) {
$output .= theme('menu_item', $link, $data['link']['has_children'], menu_tree_output($data['below']), $data['link']['in_active_trail'], $extra_class);
}
else {
$output .= theme('menu_item', $link, $data['link']['has_children'], '', $data['link']['in_active_trail'], $extra_class);
}
}
}
return theme('menu_tree', $output);
?>
I'm not sure I entirely understand the situation, but you may want to take a look at Menu Block Split which allows you to split levels of navigation into blocks. Here is an excerpt from its project page:
... split any menu block into two different blocks: a first block with the first level menu entries only and a second block with any second level and sub level menu entries. You can have as many splitted blocks as you need.

Resources