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.

This is from the WordPress site, and I can't find my answer in the documentation. When I issue the register_post_type, how does it know which function to use? I know the init action was populated with the custom hook codex_book_init, but when I look at register_post_type. It just has "book":

add_action( 'init', 'codex_book_init' );

register_post_type( 'book', $args );

How does this work? Not asking about all the $args, only the one(s) that matter for this question.

add_action( 'init', 'codex_book_init' );
/**
 * Register a book post type.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_post_type
 */
function codex_book_init() {
    $labels = array(
        'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
        'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
        'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
        'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
        'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
        'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
        'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
        'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
        'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
        'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
        'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
        'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
        'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
        'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'book' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'book', $args );
}
share|improve this question

2 Answers 2

up vote 4 down vote accepted

You are confusing between function and hook. register_post_type() is a function, not a hook.

When you are using register_post_type( 'book', $args ), you are calling the register_post_type() function to create a Custom Post Type named book.

register_post_type() needs to be used on the init hook which is fired at the beginning of wordpress initialisation, that's why you are using add_action( 'init', 'codex_book_init' ) and using register_post_type() there.

share|improve this answer
    
. Thanks. I didn't know it wasn't a hook or there was even a distinction. –  johnny Aug 15 at 17:00

book is what you're naming the post type you're registering. Then it's referencing the $args array to define properties for the the custom post type. It could just as easily be written as so:

add_action( 'init', 'codex_book_init' );

function codex_book_init() {
  register_post_type(
    'book',
     array( // Arguments
       'labels'             => array(
         'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
         'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
         'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
         'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
         'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
         'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
         'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
         'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
         'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
         'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
         'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
         'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
         'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
         'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )
       ),
       'public'             => true,
       'publicly_queryable' => true,
       'show_ui'            => true,
       'show_in_menu'       => true,
       'query_var'          => true,
       'rewrite'            => array( 'slug' => 'book' ),
       'capability_type'    => 'post',
       'has_archive'        => true,
       'hierarchical'       => false,
       'menu_position'      => null,
       'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
     )
   );
  }
share|improve this answer
    
Thanks for formatting it differently. You put function up top which makes sense or it is easier to read anyway. I see both $args and this way now. Thanks. –  johnny Aug 15 at 17:02

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.