检测声明自定义帖子类型的位置

时间:2013-06-09 作者:hereswhatidid

在我正在开发的插件中,我希望能够显示当前启用的所有自定义帖子类型的列表。是否有任何方法可以检测post类型的起始位置?例如,如果它是由一个插件创建的,是否有方法检测它,然后报告它启动的插件或文件?

1 个回复
最合适的回答,由SO网友:fuxia 整理而成

钩入registered_post_type 并存储来自debug_backtrace(). 第三个条目应该是调用插件。

示例代码

add_action( \'registered_post_type\', \'track_post_types\', 10, 2);

function track_post_types( $post_type = NULL, $args = array() )
{
    static $tracked = array();

    if ( \'shutdown\' === current_filter() )
    {
        print \'<pre>$tracked = \' . esc_html( var_export( $tracked, TRUE ) ) . \'</pre>\';
        return;
    }

    if ( $args->_builtin )
        return;

    $backtrace = debug_backtrace();
    $tracked[ $post_type ] = $backtrace[3];

    add_action( \'shutdown\', __FUNCTION__ );
}

样本输出

  array (
    \'file\' => \'F:\\\\Dropbox\\\\wp-content\\\\plugins\\\\t5-cpt-tester\\\\t5-cpt-tester.php\',
    \'line\' => 45,
    \'function\' => \'register_post_type\',
    \'args\' => 
    array (
      0 => \'project\',
      1 => 
      array (
        \'can_export\' => true,
        \'description\' => \'\',
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'hierarchical\' => false,
        \'labels\' => 
        array (
          \'add_new\' => \'Add new\',
          \'add_new_item\' => \'Add new project\',
          \'all_items\' => \'All projects\',
          \'edit_item\' => \'Edit project\',
          \'name\' => \'Projects\',
          \'name_admin_bar\' => \'Project\',
          \'new_item\' => \'New project\',
          \'not_found\' => \'No projects found.\',
          \'not_found_in_trash\' => \'No projects in trash.\',
          \'parent_item_colon\' => \'Parent project:\',
          \'search_items\' => \'Search projects\',
          \'singular_name\' => \'Project\',
          \'view_item\' => \'View project\',
        ),
        \'menu_icon\' => NULL,
        \'menu_position\' => NULL,
        \'permalink_epmask\' => 1,
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'query_var\' => \'project\',
        \'rewrite\' => 
        array (
          \'slug\' => \'project\',
          \'with_front\' => false,
        ),
        \'show_in_nav_menus\' => true,
        \'show_in_menu\' => true,
        \'show_in_admin_bar\' => true,
        \'show_ui\' => true,
        \'supports\' => 
        array (
          0 => \'author\',
          1 => \'comments\',
          2 => \'editor\',
          3 => \'excerpt\',
          4 => \'revisions\',
          5 => \'thumbnail\',
          6 => \'title\',
        ),
      ),
    ),
  )

结束

相关推荐