最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Why my CPT does not appear on the dashboard menu when i activate the plugin?

programmeradmin2浏览0评论

I've added this code to my magazine.php file into the plugin, but when it is activated, the CPT does not appear in the dashboard menu. Do you figure out why? it is a really minimalistic CPT, i do not understand why it does not work...

add_action('init', 'mc_setup_post_type');

function mc_activation(){
// trigger the wp function that registers the custom post type
mc_setup_post_type();

//clear the permalinks after the post type has been registered
flush_rewrite_rules();
}

function mc_setup_post_type(){
//register the "magazine" custom post type

register_post_type('magazine', array(
                              'labels' => array(
                                        'name' => 'magazine'),
                              'public' => 'true'));
}

I've added this code to my magazine.php file into the plugin, but when it is activated, the CPT does not appear in the dashboard menu. Do you figure out why? it is a really minimalistic CPT, i do not understand why it does not work...

add_action('init', 'mc_setup_post_type');

function mc_activation(){
// trigger the wp function that registers the custom post type
mc_setup_post_type();

//clear the permalinks after the post type has been registered
flush_rewrite_rules();
}

function mc_setup_post_type(){
//register the "magazine" custom post type

register_post_type('magazine', array(
                              'labels' => array(
                                        'name' => 'magazine'),
                              'public' => 'true'));
}
Share Improve this question edited Jun 8, 2019 at 4:45 jaze 901 gold badge1 silver badge6 bronze badges asked Jun 8, 2019 at 4:06 Raul Magdalena CatalaRaul Magdalena Catala 113 bronze badges 2
  • Is magazine.php included somewhere or it's an orphan file? Please make clear what your plugin is. – Max Yudin Commented Jun 8, 2019 at 6:10
  • @MaxYudin, the magazine.php is included into the plugin folder structure, and it is showed into the plugin wordpress directory and can be activated. Also, the register_activation_hook(), which is into the same php file, works and it creates the table needed to store the magazines records – Raul Magdalena Catala Commented Jun 8, 2019 at 7:15
Add a comment  | 

1 Answer 1

Reset to default 2

The problem is that you've set the value for 'public' incorrectly. That argument is a boolean, meaning it's true or false, and in PHP these need to be written without quotes to be correct:

register_post_type(
    'magazine',
    array(
        'labels' => array(
                'name' => 'magazine',
        ),
        'public' => true,
    )
);

For the post type to appear in the admin sidebar, 'show_in_menu' needs to be true. The default value of 'show_in_menu' — if it's not provided — is the same as 'show_ui', which itself defaults to the value of 'public'. this is why 'public' being set incorrectly was causing the problem.

The reason Pravin's answer works is because he's manually setting 'show_in_menu' to true. But while this makes the post type appear in the admin menu, it's still not set to public correctly, which means that the post type still won't be publicly visible. The 'show_in_menu' argument is not required if 'public' is set correctly.

发布评论

评论列表(0)

  1. 暂无评论