php – How do I change the order of WordPress admin menu buttons added by plugins? (some won’t move)

Question

I apologize if I’m making a stupid mistake, but I can’t get certain WordPress admin menu (left side not top bar) buttons/links to move. I know how to change the order, and have done it successfully using both of the methods shown below. The problem is the admin menu name/slug won’t work on all of the links, mostly ones that are from plugins, for example the woocomerce tab is the one that I really care about and it won’t budge, the ‘admin.php?page=wc-admin’ slug just won’t work. I’m getting the slug directly from the page. Just ‘wc-admin’ doesn’t work either.

Here are the two methods I tried, but like I said the slugs (not all of them) are the only part I’m having issues with, the code itself seems to work just fine.

add_filter('custom_menu_order', function() { return true; });
add_filter('menu_order', 'my_new_admin_menu_order');
/**
* Filters WordPress' default menu order
*/
function my_new_admin_menu_order( $menu_order ) {
// define your new desired menu positions here
$new_positions = array(
 'upload.php' => 9,
 'edit.php?post_type=product' => 1,
 'edit.php?post_type=page' => 2,
 'themes.php' => 3,
 'admin.php?page=wc-admin' => 4,
 'edit.php?post_type=shop_coupon&acfw' => 5 // This one also doesn't work
);
// helper function to move an element inside an array
function move_element(&$array, $a, $b) {
  $out = array_splice($array, $a, 1);
  array_splice($array, $b, 0, $out);
}
// traverse through the new positions and move 
// the items if found in the original menu_positions
foreach( $new_positions as $value => $new_index ) {
  if( $current_index = array_search( $value, $menu_order ) ) {
    move_element($menu_order, $current_index, $new_index);
  }
}
return $menu_order;
};


// Second method

function custom_menu_order($menu_ord) {
    if (!$menu_ord) return true;
    return array(
        'index.php',
        'edit.php?post_type=page', 
        'edit.php?post_type=product',
        'edit.php?post_type=shop_coupon&acfw',
        'themes.php',
        'admin.php'
 );
}
add_filter('custom_menu_order', 'custom_menu_order');
add_filter('menu_order', 'custom_menu_order'); 

0
Infamous11 5 months 2022-03-25T14:02:17-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse