Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In a plugin that I'm developing I'd like to be able to display a list of all custom post types currently enabled. Is there any way to detect where a post type has been initiated? For example, if it was created by a plugin, is there a way to detect that and then report the plugin or file where it was initiated?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Hook into registered_post_type and store the data from debug_backtrace(). The third entry should be the calling plugin.

Sample code

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

Sample output

  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',
        ),
      ),
    ),
  )
share|improve this answer
    
+1 elegant solution –  birgire Jun 9 '13 at 9:10
    
Amazing! Exactly what I was looking for. –  hereswhatidid Jun 9 '13 at 14:27
    
Side note: This works just as well when the post type is created via a theme. –  hereswhatidid Jun 9 '13 at 16:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.