Detect where custom post type is declared

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?

Related posts

1 comment

  1. 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',
            ),
          ),
        ),
      )
    

Comments are closed.