what hook to hook into for creating custom post types?

I hate putting things into the functions.php file and therefore wanted to hook into a pre-existing WP-hook to register/configure my custom post-types (and taxonomies). Somewhere I was reading that “template-redirect” would be a good one but it appear that that hook isn’t fired when you’re on the admin pages and therefore is fairly useless.

Any help would be appreciated.

Related posts

Leave a Reply

3 comments

  1. You can use init hook.

    An example of registering a post type called “book”.

    function codex_custom_init() {
        $args = array( 'public' => true, 'label' => 'Books' );
        register_post_type( 'book', $args );
    }
    add_action( 'init', 'codex_custom_init' );
    

    Reference: Codex.

  2. I was reviewing my open questions and it reminded me that I hadn’t closed this one out. Marty’s answer was helpful but really pointed to a different solution path. In retrospect, I’m not sure what hook I had tried but the kind of obvious one is “init” and I’m using that now and it works.

    Here’s my flow:

    • My plugin is loaded with the ‘plugins_loaded’ event
    • It does some basic initialisation and then hooks into the ‘admin’ hook
    • When the ‘admin’ event is fired my plugin then this fires off the following function:

      function add_hooks () {
          // fire a hook that a configuration file can pick up
          do_action ( 'lg_custom_types_definition');
          // now fire hooks to register custom types
          do_action ( 'lg_custom_type_cpt_registration' );        // register
          do_action ( 'lg_custom_types_registered_post_types');
          do_action ( 'lg_custom_type_tax_registration' );        // register
          do_action ( 'lg_custom_types_registered_taxonomies');
      }
      

    This approach gives me a completely decoupled approach which means I can enable the “custom_types” plugin and I now have the ‘capability’ installed. Then I create a configuration plugin that hooks into the events that the capability has added.

    Hope this helps.

  3. You could use an include file within functions.php to include all your custom work.

    <?php 
    // functions.php
    include('inc/custom-functions.php');
    ?>
    

    I’ve created a very simple page to create your custom post types,
    you input the options you want for the custom field and it spits out the code needed in order to generate it in wordpress..

    its located here: http://martin-gardner.co.uk/wordpress-custom-post-type-generator/

    for example:

    • Input the Post Type Name: Video select where in the menu you want it positioned.
    • then select the options you want for that custom post type.
    • Edit labels, if you want, by default just uses name,

    Read more on custom post types and the register_post_type @

    http://codex.wordpress.org/Function_Reference/register_post_type

    hope that helps a little 😉

    Marty