WordPress:$ _registered_pa​​ges和register_post_type

时间:2014-09-27 02:49:10

标签: php wordpress

自定义插件菜单结构:

  

测试插件

     
    

仪表板

         

礼品

  

我有自定义帖子类型突出显示问题,如果您点击Add [post type]链接,如果找不到Add New [post type]项目,则会默认为顶级菜单项菜单。请参阅以下链接以获得澄清:https://core.trac.wordpress.org/ticket/24137

问题在几个月前得到解决,然而,当我进入wp-admin/post-new.php转出$_registered_pages所包含的内容时,我发现了一些不一致的问题。我使用WooCommerce作为比较的手段。这是$_registered_pages包含的内容的转储:

array(16) { ["admin_page_testplugin"]=> bool(true) ["toplevel_page_woocommerce"]=> bool(true) ["product_page_product_attributes"]=> bool(true) ["woocommerce_page_edit?post_type=shop_order"]=> bool(true) ["woocommerce_page_edit?post_type=shop_coupon"]=> bool(true) ["admin_page_edit?post_type=gifts"]=> bool(true) ["users_page_users-user-role-editor"]=> bool(true) ["settings_page_settings-user-role-editor"]=> bool(true) ["toplevel_page_testplugin"]=> bool(true) ["appearance_page_custom-header"]=> bool(true) ["appearance_page_custom-background"]=> bool(true) ["woocommerce_page_wc-reports"]=> bool(true) ["woocommerce_page_wc-settings"]=> bool(true) ["woocommerce_page_wc-status"]=> bool(true) ["woocommerce_page_wc-addons"]=> bool(true) ["appearance_page_theme-editor"]=> bool(true) }

我注意到它为WooCommerce输出woocommerce_page_edit,但为我自己的自定义帖子类型(礼物)输出admin_page_edit ...即使它应该以{{1​​}}形式出现。这意味着,当testplugin_page_edit?post_type=gifts中的以下代码get_plugin_page_hookname( "edit.php?post_type=$post_type", $post_type_object->show_in_menu )运行时,它将永远找不到它,因为它将寻找wp-admin/post-new.php,但不幸的是,它不存在。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

经过大约一天的实验和玩弄代码后,似乎就是在priority函数上设置特定add_action的情况。对于将来可能想知道相同的人,以下是完整的代码。

<?php defined('ABSPATH') or exit;
/**
 * Plugin Name: Testa
 * Description: Testing 123
 * Version: 1.0
 */
function menus() {
    add_menu_page( 'Testa', 'Testa', 'manage_options', 'testa', null );
    add_submenu_page( 'testa', 'Dashboard', 'Dashboard', 'manage_options', 'testa', null, null, 9 );
}

add_action( 'admin_menu', 'menus', 9 );

function post_types() {
    register_post_type( 'gifts', array(
        'labels' => array(
            'name'                  => 'Gifts',
            'singular_name'         => 'Gift',
            'add_new'               => 'Add Gift',
            'add_new_item'          => 'Add New Gift',
            'edit'                  => 'Edit',
            'edit_item'             => 'Edit Gift',
            'new_item'              => 'New Gift',
            'view'                  => 'View Gift',
            'view_item'             => 'View Gift',
            'search_items'          => 'Search Gifts',
            'not_found'             => 'No gifts found.',
            'not_found_in_trash'    => 'No gifts found in Trash.',
            'parent'                => 'Parent Gifts',
            'menu_name'             => 'Gifts',
        ),
        // front-end
        'public'                => false,
        'has_archive'           => false,
        'publicly_queryable'    => false,

        // admin
        'capability_type'       => 'post',
        'query_var'             => true,
        'show_in_menu'          => 'testa',
        'show_ui'               => true,
    ) );
}

add_action( 'init', 'post_types', 5 );