Last active
March 7, 2024 13:00
-
-
Save jonathanbossenger/24da0addd61eb6fef1fa95ce78f9fb6d to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Plugin Name: Bookstore | |
* Description: A plugin to manage books | |
* Version: 1.0 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly | |
} | |
add_action( 'init', 'bookstore_register_book_post_type' ); | |
function bookstore_register_book_post_type() { | |
$args = array( | |
'labels' => array( | |
'name' => 'Books', | |
'singular_name' => 'Book', | |
'menu_name' => 'Books', | |
'add_new' => 'Add New Book', | |
'add_new_item' => 'Add New Book', | |
'new_item' => 'New Book', | |
'edit_item' => 'Edit Book', | |
'view_item' => 'View Book', | |
'all_items' => 'All Books', | |
), | |
'public' => true, | |
'has_archive' => true, | |
'show_in_rest' => true, | |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields' ), | |
); | |
register_post_type( 'book', $args ); | |
// Add an isbn custom field to the book post type | |
register_meta( | |
'post', | |
'isbn', | |
array( | |
'object_subtype' => 'book', | |
'show_in_rest' => true, | |
'single' => true, | |
'type' => 'string', | |
) | |
); | |
} | |
add_action( 'init', 'bookstore_register_genre_taxonomy' ); | |
function bookstore_register_genre_taxonomy() { | |
$args = array( | |
'labels' => array( | |
'name' => 'Genres', | |
'singular_name' => 'Genre', | |
'edit_item' => 'Edit Genre', | |
'update_item' => 'Update Genre', | |
'add_new_item' => 'Add New Genre', | |
'new_item_name' => 'New Genre Name', | |
'menu_name' => 'Genre', | |
), | |
'hierarchical' => true, | |
'rewrite' => array( 'slug' => 'genre' ), | |
'show_in_rest' => true, | |
); | |
register_taxonomy( 'genre', 'book', $args ); | |
} | |
add_filter( 'postmeta_form_keys', 'bookstore_add_isbn_to_quick_edit', 10, 2 ); | |
function bookstore_add_isbn_to_quick_edit( $keys, $post ) { | |
if ( $post->post_type === 'book' ) { | |
$keys[] = 'isbn'; | |
} | |
return $keys; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment