1

I want to add two additional sub-menu items to my Wordpress Admin menu. The top-level menu I want to hook into is the 'Products' menu created by WooCommerce.

edit.php?post_type=product

The content I want the menu items to show can be accessed by filtering the products by Product Category. E.g.

http://dev3.benefacto.org/wp-admin/edit.php?s&post_type=product&product_cat=manchester

I've come up with a working solution (below) to do this - but it is inelegant because it requires calling a function when I feel like I should be able to simply add something into the 'menu slug' variable perhaps.

Any thoughts much appreciated.

// Hook into the Admin Menu

add_action( 'admin_menu', 'lnz_wp_adminmenu_addproductpages' );

// Add Product Categories
function lnz_wp_adminmenu_addproductpages() {
add_submenu_page( 'edit.php?post_type=product', 'Manchester Charities - Page', 'Manchester Charities- Menu', 'manage_options', 'product_cat_manchester', 'lnz_wp_adminmenu_redirectmanchester' );
add_submenu_page( 'edit.php?post_type=product', 'London Charities - Page', 'London Charities- Menu', 'manage_options', 'product_cat_london', 'lnz_wp_adminmenu_redirectlondon' ); 
}

// Create Redirects for relevant links
function lnz_wp_adminmenu_redirectmanchester() {
   header('Location: http://dev3.benefacto.org/wp-admin/edit.php?s&post_type=product&product_cat=manchester');
   exit();
}

function lnz_wp_adminmenu_redirectlondon() {
   header('Location: http://dev3.benefacto.org/wp-admin/edit.php?s&post_type=product&product_cat=london');
   exit();
}
Linz Darlington
  • 515
  • 1
  • 10
  • 25

1 Answers1

1

As far as I know, there's no direct way to achieve this using WP hooks or modifying something like global $submenu...

It can be done with jQuery modifying the href attribute of the submenu items:

add_action( 'admin_menu', function() {
    add_submenu_page( 'edit.php?post_type=product', 'Manchester', 'Manchester', 'manage_options', 'cat_manchester', '__return_null' );
    add_submenu_page( 'edit.php?post_type=product', 'London', 'London', 'manage_options', 'cat_london', '__return_null' ); 
});

add_action('admin_footer', function(){
    ?>
    <script>
        jQuery(document).ready( function($) {
            var admin_url = 'http://dev3.benefacto.org/wp-admin/edit.php';
            $('#menu-posts-product').find('a[href*="cat_manchester"]').attr('href',admin_url+'?s&post_type=product&product_cat=manchester');
            $('#menu-posts-product').find('a[href*="cat_london"]').attr('href',admin_url+'?s&post_type=product&product_cat=london');
        });
    </script>
    <?php
});
brasofilo
  • 25,496
  • 15
  • 91
  • 179