Adding Admin Menu Separators in WordPress
Asked Answered
F

2

6

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
Flavoprotein answered 16/10, 2013 at 23:3 Comment(1)
I'm guessing you haven't applied an array, hence why ksort() is reporting that a null has been given.. Since no array has been processed by the foreach loop, it will generate the warningVadavaden
G
6

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?

Geller answered 16/10, 2013 at 23:58 Comment(0)
H
1

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.

Hemicellulose answered 7/8, 2020 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.