Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Subscribe to RSS feed


Reordering admin menu in WordPress

Stanislav KhromovStanislav Khromov

Here’s a quick snippet to reorder the admin menu in WordPress.

Simply specify the order you want in the $reordered_items array (you can var_dump($menu_order) to find all available menu items.

The items will be inserted under the topmost link (Dashboard).

add_filter('menu_order', 'reorder_admin_menu', 999);

/**
 * Reorders admin menu to match the wanted order
 *
 * @param $menu_order
 * @return mixed
 */
function reorder_admin_menu($menu_order) {

  //Example. Puts "Pages" above "Posts".
  $reordered_items = array(
    'edit.php?post_type=page',
    'edit.php'
  );

  //This is where we will insert our reordered items
  $reordered_items_insertion_point = 'index.php';

  //Remove items we are supposed to reorder
  $filtered_menu_order = array_diff($menu_order, $reordered_items);

  //Init new order
  $new_menu_order = array();

  //Loop all current menu items
  foreach($filtered_menu_order as $menu_item) {

    //Add to array
    $new_menu_order[] = $menu_item;

    //Our trigger? Let's go!
    if($menu_item === $reordered_items_insertion_point) {

      //Add in our reordered items
      $new_menu_order = array_merge($new_menu_order, $reordered_items);
    }
  }

  return $new_menu_order;
}
PHP

Full-stack impostor syndrome sufferer & Software Engineer at Schibsted Media Group

Comments 0
There are currently no comments.