Reorder custom submenu item

In the Settings menu, I have the following menu items listed:

Settings
-- General
-- Writing
-- Reading
-- Discussion
-- Media
-- Permalinks
-- Blogging

I’d like to the Blogging (options-general.php?page=blogging) reordered underneath General instead of being at the bottom. This was added with the add_options_page() function.

From doing some research, this is what I’ve come up with:

add_filter( 'custom_menu_order', array( $this, 'submenu_order' ) );
function submenu_order( $menu_order ) {
    global $submenu;
    $order = array();
    $order[] = $submenu['options-general.php'][10];
    $order[] = $submenu['options-general.php'][41];
    $submenu['options-general.php'] = $order;
    return $menu_order;
}

This works, but it only shows General and Blogging, the rest are removed:

Settings
-- General
-- Blogging

Also, $submenu['options-general.php'][41] is currently index position 41 for me. Does this mean it will be the same index position for everyone else even if they have another plugin settings listed?

2 Answers
2

Got it, thanks to cjbj’s help, I was able to get the final solution:

add_filter( 'custom_menu_order', 'submenu_order' );
function submenu_order( $menu_order ) {
    # Get submenu key location based on slug
    global $submenu;
    $settings = $submenu['options-general.php'];
    foreach ( $settings as $key => $details ) {
        if ( $details[2] == 'blogging' ) {
            $index = $key;
        }
    }
    # Set the 'Blogging' menu below 'General'
    $submenu['options-general.php'][11] = $submenu['options-general.php'][$index];
    unset( $submenu['options-general.php'][$index] );
    # Reorder the menu based on the keys in ascending order
    ksort( $submenu['options-general.php'] );
    # Return the new submenu order
    return $menu_order;
}

Leave a Comment