【问题标题】:Directing to filtered pages from Wordpress Admin Sub Menus从 Wordpress 管理子菜单定向到过滤页面
【发布时间】:2017-11-10 11:54:50
【问题描述】:

我想在我的 Wordpress 管理菜单中添加两个额外的子菜单项。我想挂钩的顶级菜单是 WooCommerce 创建的“产品”菜单。

edit.php?post_type=product

我希望菜单项显示的内容可以通过按产品类别过滤产品来访问。例如

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

我已经提出了一个可行的解决方案(如下)来做到这一点 - 但它并不优雅,因为当我觉得我应该能够简单地将一些东西添加到“menu slug”变量中时,它需要调用一个函数。

任何想法都非常感谢。

// 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();
}

【问题讨论】:

    标签: php wordpress menu admin


    【解决方案1】:

    据我所知,没有直接的方法可以使用 WP 挂钩或修改类似 global $submenu...

    可以通过jQuery修改子菜单项的href属性来完成:

    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
    });
    

    【讨论】:

    • 谢谢。明天我会试一试,然后回头看看它是如何工作的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多