douzhanyan5015 2018-05-02 00:36
浏览 48
已采纳

在“功能插件”中为自定义帖子类型注册分类法会导致错误500

I'm moving functionality from a theme's functions.php to a "functionality plugin". For some reason, when in functions.php, everything works perfectly fine. But when I move it into plugin, I get error 500 for the entire wp-admin area.

I was able to pinpoint problem to taxonomy registration block of code by excluding functionality blocks piece by piece.

Here is the whole custom post type registration code:

function create_post_type() {
  register_post_type( 'Ads',
    array(
      'labels' => array(
        'name'               => 'Ads',
        'singular_name'      => 'Ad',
        'menu_name'          => 'Ads',
        'name_admin_bar'     => 'Ad',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Ad',
        'new_item'           => 'New Ad',
        'edit_item'          => 'Edit Ad',
        'view_item'          => 'View Ad',
        'all_items'          => 'All Ads',
        'search_items'       => 'Search Ads',
        'parent_item_colon'  => 'Parent Ads:',
        'not_found'          => 'No ads found.',
        'not_found_in_trash' => 'No ads found in Trash.'
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => true,
      'hierarchical' => true,
      'supports' => array( 
        'title', 
        'revisions'
    ),
    )
  );
}
/* This piece bellow is causing trouble*/
register_taxonomy("Placements", array("ads"), array(
    "hierarchical" => true,
    "label" => "Placements",
    "singular_label" => "Placement",
    "rewrite" => true
));
/* End of trouble making code */
add_action( 'init', 'create_post_type' );

and here is the error log

[Tue May 01 23:11:04.268105 2018] [:error] [pid 24511] [client 174.214.1.78:15113] PHP Fatal error: Uncaught Error: Call to a member function add_rewrite_tag() on null in /nas/content/live/adsplugintest/wp-includes/rewrite.php:172 Stack trace: #0 /nas/content/live/adsplugintest/wp-includes/class-wp-taxonomy.php(379): add_rewrite_tag('%Placements%', '([^/]+)', 'placements=') #1 /nas/content/live/adsplugintest/wp-includes/taxonomy.php(386): WP_Taxonomy->add_rewrite_rules() #2 /nas/content/live/adsplugintest/wp-content/plugins/custom-ads-plugin/custom-ads.php(45): register_taxonomy('Placements', Array, Array) #3 /nas/content/live/adsplugintest/wp-settings.php(305): include_once('/nas/content/li...') #4 /nas/content/live/adsplugintest/wp-config.php(121): require_once('/nas/content/li...') #5 /nas/content/live/adsplugintest/wp-load.php(37): require_once('/nas/content/li...') #6 /nas/content/live/adsplugintest/wp-admin/admin.php(31): require_once('/nas/content/li...') #7 /nas/content/live/adsplugintest/wp-admin/index.php(10): require_once('/nas/content/li...') #8 {main} thrown in /nas/content/live/adsplugintest/wp-includes/rewrite.php on line 172

  • 写回答

1条回答 默认 最新

  • dongqiaogouk86049 2018-05-02 01:42
    关注

    You need to call the register_taxonomy() method as part of the init action (as you've done for register_post_type().

    Updated code would be:

    function create_post_type() {
        register_post_type( ... );
        register_taxonomy( ... );
    }
    add_action( 'init', 'create_post_type' );
    

    At the moment, you're calling register_taxonomy() directly instead of via the init action, so it's firing too soon for WordPress to handle it appropriately.

    The WordPress Codex for register_taxonomy() also mentions this:

    Use the init action to call this function. Calling it outside of an action can lead to troubles. See #15568 for details.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题