Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Admin Menu Separators in WordPress

I am trying to create an admin menu separator that allows you to put them in with code. This is the function:

function add_admin_menu_separator($position) {
  global $menu;
  $index = 0;
  foreach($menu as $offset => $section) {
    if (substr($section[2],0,9)=='separator')
    $index++;
    if ($offset>=$position) {
      $menu[$position] = array('','read',"separator{$index}",'','wp-menu-separator');
      break;
    }
  }
  ksort( $menu );
}

The add action bit is below

add_action('admin_init','admin_menu_separator');
    
function admin_menu_separator() {
  add_admin_menu_separator(220);
}

It works okay but it produces the following errors in WordPress when rearranging menus:

> Warning: Invalid argument supplied for foreach() in /home/user/public_html/wp-creation.com/wp-content/themes/liquid_theme_0.4_licensed/functions.php on line 174
    
> Warning: ksort() expects parameter 1 to be array, null given in /home/user/public_html/wp-creation.com/wp-content/themes/liquid_theme_0.4_licensed/functions.php on line 182
like image 931
user102945 Avatar asked Oct 16 '13 23:10

user102945


People also ask

How do I add a separator to my WordPress menu?

Adding WordPress Menu Separator In the Custom links block add a new menu item with the same name. Add # hash symbol. Add # hash symbol into the link field to create a non-clickable menu item. Click on Add to Menu button to add the non-clickable item (separator) to your existing menu.


2 Answers

You should hook in admin_menu:

add_action('admin_menu','admin_menu_separator');

And use something lower than 220. The biggest offset I got in my system is 99.

Check this very fine class to deal with Admin Menus.
It appeared in this WPSE Question: Add a Separator to the Admin Menu?

like image 176
brasofilo Avatar answered Sep 22 '22 12:09

brasofilo


You can simply add this in functions.php

add_action('admin_menu', function () {   
  global $menu;
  $menu[49] = ['', 'read', '', '', 'wp-menu-separator'];
});

Where 49 is separator position, You have to remember that separator position can replace your menu element if it has the same position.

like image 32
Jankyz Avatar answered Sep 18 '22 12:09

Jankyz