Skip to content

Instantly share code, notes, and snippets.

@hellofromtonya hellofromtonya/books.php

Last active Mar 19, 2019
Embed
What would you like to do?
Show Custom Taxonomy Panel in the Gutenberg Editor
<?php
/**
* Adds Books to your website.
*
* @package KnowTheCode\Books
* @author hellofromTonya
* @license GPL-2.0+
*
* @wordpress-plugin
* Plugin Name: Books
* Plugin URI: https://KnowTheCode.io
* Description: Adds books to your website.
* Version: 1.0.0
* Author: hellofromTonya
* Author URI: https://KnowTheCode.io
* Text Domain: books
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* GitHub Plugin URI: https://KnowTheCode.io
* Requires PHP: 5.6
* Requires WP: 4.9
*/
namespace KnowTheCode\Books;
add_action( 'init', __NAMESPACE__ . '\register_book_post_type' );
/**
* Register a Book custom post type.
*
* @since 1.0.0
*/
function register_book_post_type() {
$labels = [
'name' => _x( 'Books', 'post type general name', 'books' ),
'singular_name' => _x( 'Book', 'post type singular name', 'books' ),
'menu_name' => _x( 'Books', 'admin menu', 'books' ),
'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'books' ),
'add_new' => _x( 'Add New', 'book', 'books' ),
'add_new_item' => __( 'Add New Book', 'books' ),
'new_item' => __( 'New Book', 'books' ),
'edit_item' => __( 'Edit Book', 'books' ),
'view_item' => __( 'View Book', 'books' ),
'all_items' => __( 'All Books', 'books' ),
'search_items' => __( 'Search Books', 'books' ),
'parent_item_colon' => __( 'Parent Books:', 'books' ),
'not_found' => __( 'No books found.', 'books' ),
'not_found_in_trash' => __( 'No books found in Trash.', 'books' ),
];
$args = [
'labels' => $labels,
'description' => __( 'Cool books.', 'books' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_rest' => true, // To use Gutenberg editor.
'query_var' => true,
'rewrite' => [ 'slug' => 'book' ],
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => [ 'title', 'editor', 'author', 'thumbnail', 'excerpt' ],
];
register_post_type( 'book', $args );
}
add_action( 'init', __NAMESPACE__ . '\register_genre_taxonomy', 0 );
/**
* Register the Genre custom taxonomy.
*
* @since 1.0.0
*/
function register_genre_taxonomy() {
$labels = [
'name' => _x( 'Genres', 'taxonomy general name', 'books' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name', 'books' ),
'search_items' => __( 'Search Genres', 'books' ),
'all_items' => __( 'All Genres', 'books' ),
'parent_item' => __( 'Parent Genre', 'books' ),
'parent_item_colon' => __( 'Parent Genre:', 'books' ),
'edit_item' => __( 'Edit Genre', 'books' ),
'update_item' => __( 'Update Genre', 'books' ),
'add_new_item' => __( 'Add New Genre', 'books' ),
'new_item_name' => __( 'New Genre Name', 'books' ),
'menu_name' => __( 'Genre', 'books' ),
];
$args = [
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'show_in_rest' => true, // Needed for tax to appear in Gutenberg editor.
'query_var' => true,
'rewrite' => [ 'slug' => 'genre' ],
];
register_taxonomy( 'genre', [ 'book' ], $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.