This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
WordPress hooks are like the secret ingredients in our favorite recipes – they spice things up and allow us to customize and extend our WordPress sites without altering the core files. One such hook is the custom_menu_order filter, which lets us customize the order of the WordPress admin menu items. Let’s dive into how you can use this hook to achieve your desired menu order.
Example 1: Setting Custom Menu Order
This example shows how to use the custom_menu_order filter to set a custom order for your admin menu items.
add_filter( 'custom_menu_order', '__return_true' ); add_filter( 'menu_order', 'weplugins_my_menu_order' ); function weplugins_my_menu_order( $menu_order ) { return array( 'index.php', 'edit.php', 'edit.php?post_type=page', 'edit-comments.php' ); }
Example 2: Modifying Custom Menu Order Defaults
Here’s how you can modify the default settings for the custom menu order using a custom function.
function weplugins_modify_custom_menu_order_defaults($custom) { // Update the $custom variable according to your website requirements and return this variable. // You can modify the $custom variable conditionally too if you want. return $custom; } // add the filter add_filter( "custom_menu_order", "weplugins_modify_custom_menu_order_defaults", 10, 1 );
Example 3: Removing the Custom Menu Order Hook
Need to remove the custom menu order hook? Use the code snippet below.
remove_filter( "custom_menu_order", "weplugins_modify_custom_menu_order_defaults", 10, 1 );
Make sure to provide the same callback function name, priority, and number of arguments when removing the hook callback.
Explore the latest in WordPress
Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.