I believed that is already set to init
, here’s my code.
/**
* $post post.
*
* @var $this Controller.
*/
protected $posts;
/**
* Register Custom Post Types.
*/
public function register_post_types() {
foreach ( $this->posts as $name => $value ) {
if ( ! apply_filters( "backdrop/post/type/{$name}", false ) ) {
register_post_type( $name, $value );
}
}
}
/**
* Create Posts by create_post_type().
*
* @param string $type a post type.
* @param string $singular_label a single label.
* @param string $plural_label a more than one.
*/
public function create_post_type( $type, $singular_label, $plural_label ) {
$labels = [
'name' => sprintf( esc_html__( '%s', 'backdrop-post-types' ), $plural_label ),
'singular_name' => sprintf( esc_html__( '%s', 'backdrop-post_types' ), $singular_label ),
'name_admin_bar' => sprintf( esc_html__( '%s', 'backdrop-post_types' ), $singular_label ),
'add_new' => sprintf( esc_html__( 'New %s', 'backdrop-post-types' ), $singular_label ),
'add_new_item' => sprintf( esc_html__( 'Add New %s', 'backdrop-post-types' ), $singular_label ),
'edit_item' => sprintf( esc_html__( 'Edit %s', 'backdrop-post-types' ), $singular_label ),
'new_item' => sprintf( esc_html__( 'New %s', 'backdrop-post-types' ), $singular_label ),
'view_item' => sprintf( esc_html__( 'View %s', 'backdrop-post-types' ), $singular_label ),
'search_items' => sprintf( esc_html__( 'Search %s', 'backdrop-post-types' ), $plural_label ),
'not_found' => sprintf( esc_html__( 'Not %s Found', 'backdrop-post-types' ), $plural_label ),
'not_found_in_trash' => sprintf( esc_html__( 'Not %s Found in Trash', 'backdrop-post-types' ), $plural_label ),
];
$args = [
'labels' => $labels,
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-category',
'show_ui' => true,
'show_in_rest' => true,
'supports' => apply_filters( "backdrop/post/type/supports/{$type}", [ 'title', 'editor', 'thumbnail' ]),
'taxonomies' => [ $type . '_category', $type . '_tag' ],
'rewrite' => [ 'with_front' => false, 'slug' => $type ]
];
$this->posts[ 'backdrop-' . $type ] = array_merge( $labels, $args );
}
public function register_category() {
foreach ( $this->posts as $key => $value ) {
$this->create_category( $key );
}
}
public function create_category( $type ) {
$labels = [
'name' => _x( 'Categories', 'Taxonomy General Name', 'backdrop-post-types' ),
'singular_name' => _x( 'Category', 'Taxonomy Singular Name', 'backdrop-post-types' ),
'menu_name' => __( 'Categories', 'backdrop-post-types' ),
'all_items' => __( 'All Categories', 'backdrop-post-types' ),
'parent_item' => __( 'Parent Category', 'backdrop-post-types' ),
'parent_item_colon' => __( 'Parent Category:', 'backdrop-post-types' ),
'new_item_name' => __( 'New Category Name', 'backdrop-post-types' ),
'add_new_item' => __( 'Add New Category', 'backdrop-post-types' ),
'edit_item' => __( 'Edit Categories', 'backdrop-post-types' ),
'update_item' => __( 'Update Categories', 'backdrop-post-types' ),
'view_item' => __( 'View Categories', 'backdrop-post-types' ),
'separate_items_with_commas' => __( 'Separate categories with commas', 'backdrop-post-types' ),
'add_or_remove_items' => __( 'Add or remove categories', 'backdrop-post-types' ),
'choose_from_most_used' => __( 'Choose from the most used', 'backdrop-post-types' ),
'popular_items' => __( 'Popular Categories', 'backdrop-post-types' ),
'search_items' => __( 'Search Categories', 'backdrop-post-types' ),
'not_found' => __( 'Not Found', 'backdrop-post-types' ),
];
$args = [
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
'show_in_rest' => true,
];
register_taxonomy( $type . '_category', array( $type ), $args );
}
public function register_tag() {
foreach ( $this->posts as $key => $value ) {
$this->create_tag( $key );
}
}
public function create_Tag( $type ) {
$labels = [
'name' => _x( 'Tags', 'Taxonomy General Name', 'backdrop-post-types' ),
'singular_name' => _x( 'Tag', 'Taxonomy Singular Name', 'backdrop-post-types' ),
'menu_name' => __( 'Tags', 'backdrop-post-types' ),
'all_items' => __( 'All Tags', 'backdrop-post-types' ),
'parent_item' => __( 'Parent Tag', 'backdrop-post-types' ),
'parent_item_colon' => __( 'Parent Tag:', 'backdrop-post-types' ),
'new_item_name' => __( 'New Tag Name', 'backdrop-post-types' ),
'add_new_item' => __( 'Add New Tag', 'backdrop-post-types' ),
'edit_item' => __( 'Edit Tags', 'backdrop-post-types' ),
'update_item' => __( 'Update Tags', 'backdrop-post-types' ),
'view_item' => __( 'View Tags', 'backdrop-post-types' ),
'separate_items_with_commas' => __( 'Separate Tags with commas', 'backdrop-post-types' ),
'add_or_remove_items' => __( 'Add or remove Tags', 'backdrop-post-types' ),
'choose_from_most_used' => __( 'Choose from the most used', 'backdrop-post-types' ),
'popular_items' => __( 'Popular Tags', 'backdrop-post-types' ),
'search_items' => __( 'Search Tags', 'backdrop-post-types' ),
'not_found' => __( 'Not Found', 'backdrop-post-types' ),
];
$args = [
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
'show_in_rest' => true,
];
register_taxonomy( $type . '_tag', array( $type ), $args );
}
/**
* Construct.
*/
public function boot() {
add_action( 'init', [ $this, 'register_post_types' ] );
add_action( 'init', [ $this, 'register_category' ] );
add_action( 'init', [ $this, 'register_tag' ] );
}
}