Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Last active January 20, 2017 16:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpsmith/6322626 to your computer and use it in GitHub Desktop.
Save wpsmith/6322626 to your computer and use it in GitHub Desktop.
PHP: Register Custom Post Type
<?php
add_action( 'init', 'gs_books_label_rename', 999 );
/**
* Modify registered post type menu label
*
*/
function gs_books_label_rename() {
global $wp_post_types;
$wp_post_types['gs_books']->labels->menu_name = __( 'Books', 'gs_books' );
}
<?php
add_action( 'registered_post_type', 'gs_books_label_rename', 10, 2 );
/**
* Modify registered post type menu label
*
* @param string $post_type Registered post type name.
* @param array $args Array of post type parameters.
*/
function gs_books_label_rename( $post_type, $args ) {
if ( 'gs_books' === $post_type ) {
global $wp_post_types;
$args->labels->menu_name = __( 'Books', 'gs_books' );
$wp_post_types[ $post_type ] = $args;
}
}
<?php
add_action( 'init', 'gs_register_books_cpt' );
/**
* Register Books Custom Post Type
*/
function gs_register_books_cpt() {
// change 'gs_books' to whatever your text_domain is.
/** Setup labels */
$labels = array(
'name' => x_( 'Books', 'gs_books' ),
'singular_name' => x_( 'Book', 'gs_books' ),
'add_new' => x_( 'Add New', 'gs_books' ),
'all_items' => x_( 'All Books', 'gs_books' ),
'add_new_item' => x_( 'Add New Book', 'gs_books' ),
'edit_item' => x_( 'Edit Book', 'gs_books' ),
'new_item' => x_( 'New Book', 'gs_books' ),
'view_item' => x_( 'View Book', 'gs_books' ),
'search_items' => x_( 'Search Books', 'gs_books' ),
'not_found' => x_( 'No Books found', 'gs_books' ),
'not_found_in_trash' => x_( 'No Books found in trash', 'gs_books' ),
'parent_item_colon' => x_( 'Parent Book:', 'gs_books' ),
'menu_name' => x_( 'Amazon Books', 'gs_books' )
);
/** Setup args */
$args = array(
'labels' => $labels,
'description' => x_( 'Amazon Books post type', 'gs_books' ),
'public' => true,
'menu_position' => 20,
'supports' => array( 'title', 'editor', 'excerpt', 'page-attributes', ),
'has_archive' => 'books',
'rewrite' => array( 'slug' => 'book', ),
);
/** Register Custom Post Type */
register_post_type( 'gs_books', $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment