Last active
September 5, 2024 01:41
-
-
Save wpsmith/6322626 to your computer and use it in GitHub Desktop.
PHP: Register Custom Post Type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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