PHP function for adding admin menu separator inside WordPress backend

WordPress admin menu separatorOften your WordPress plugin or theme adds admin submenu inside WordPress admin interface using add_menu_page() functions on admin_menu hook or using WordPress Settings API. When your code adds multiple submenu items wouldn't it be nice to create section on admin menu to reduce clutter? Unfortunately WordPress currently doesn't support adding admin menu separators so you must do it manually by filtering $menu global variable. Here's function for doing that together with an example that should demonstrate it's use.

WordPress admin menu separator after Inside function in question there is support for adding menu separator before or after given menu item slug. Please note that adding separator requires running this function after you add menu item that requires separator. As an example let's imagine we're adding two admin menu items, "First Item" and "Second Item", and that it is required to separate them by admin menu separator. Here's sample WordPress plugin together with the PHP function code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/*
Plugin Name: Admin Menu Separator
Plugin URI: http://www.techytalk.info/
Description: Demonstrates adding admin menu separator.
Version: 1.00
Author: Marko Martinović
Author URI: http://www.techytalk.info
License: GPLv2 or later
*/
 
/**
 * Add admin menu separator before or after given menu item as identified by slug
 *
 * @param string $slug Admin menu item slug
 * @param string $mode Can be 'before' or 'after', by default it is 'before'
 */
function add_admin_menu_separator($slug, $mode = 'before') {
    global $menu;
 
    $count = 0;
    // Admin menu iterator
    foreach($menu as $section) {
 
        // Find given slug
        if($section[2] == $slug){
 
            // Before or after
            if($mode == 'after')
                $count++;
 
            // Part of the menu before given slug
            $new_menu = array_slice($menu, 0, $count, true);
 
            // Add separator
            $new_menu[] = array('', 'read', 'separator'.$count, '', 'wp-menu-separator');
 
            // Part of the menu after given slug
            $after = array_slice($menu, $count, null, true);
            foreach ($after as $aoffset => $asection){
                $new_menu[$aoffset+1] = $asection;
            }
 
            // Overwrite old menu with our new menu
            $menu = $new_menu;
            break;
        }
        $count++;
    }
}
 
/**
 * admin_menu hook callback
 */
function add_admin_menu() {
    // Add first menu item
    add_menu_page('First Item', 'First Item', 'read', 'first-item-slug', 'first_item_callback');
    // Add second menu item
    add_menu_page('Second Item', 'Second Item', 'read', 'second-item-slug', 'second_item_callback');
 
    // Add admin menu separator before first item
    add_admin_menu_separator('first-item-slug', 'before');
 
    // Add admin menu separator after second item
    add_admin_menu_separator('second-item-slug', 'after');
}
add_action('admin_menu', 'add_admin_menu');
 
/**
 * First menu item dummy callback
 */
function first_item_callback() { }
 
 
/**
 * Second menu item dummy callback
 */
function second_item_callback() { }
?>
DevGenii

A quality focused Magento specialized web development agency. Get in touch!

3 thoughts on “PHP function for adding admin menu separator inside WordPress backend

Leave a Reply to sam Cancel reply

Your email address will not be published. Required fields are marked *