Last active
September 3, 2020 13:59
-
-
Save VicVicos/bd21a0bc2a6a9cada91abfd0d2e89175 to your computer and use it in GitHub Desktop.
New Post type and Taxonomy WP
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 | |
| class NewPostType | |
| { | |
| const LABEL = 'label'; | |
| const LABELS = 'labels'; | |
| const VALUE = 'value'; | |
| const DESCRIPTION = 'description'; | |
| const TYPE = 'type'; | |
| const BACK_DOMAIN = 'art-fields'; | |
| const POST_TYPE = 'art-gallery'; | |
| const TAX_TYPE = 'case'; | |
| public function init() | |
| { | |
| add_action('init', [$this, 'registerPostType'], 0); | |
| add_action('init', [$this, 'createGalleryTax'], 0); | |
| add_action('init', [$this, 'registerTaxonomy'], 0); | |
| add_action('init', [$this, 'post_for_taxonomy'], 0); | |
| add_action('post_type_link', [$this, 'galleryPermalink'], 10, 2); | |
| add_filter('post_updated_messages', [$this, 'true_post_type_messages']); | |
| // add_action('init', [$this, 'unregister']); | |
| } | |
| public function registerPostType () | |
| { | |
| $labels = array( | |
| 'name' => __('Gallery', 'art-fields'), | |
| 'singular_name' => __('Gallery item', 'art-fields'), | |
| 'add_new' => __('Add Gallery', 'art-fields'), | |
| 'add_new_item' => __('Add new item', 'art-fields'), | |
| 'edit_item' => __('Edit item', 'art-fields'), | |
| 'new_item' => __('New item', 'art-fields'), | |
| 'all_items' => __('Galleries', 'art-fields'), | |
| 'view_item' => __('View item', 'art-fields'), | |
| 'search_items' => __('Search item', 'art-fields'), | |
| 'not_found' => __('Not found', 'art-fields'), | |
| 'not_found_in_trash' => __('There are no items in the cart', 'art-fields'), | |
| 'menu_name' => __('Gallery', 'art-fields'), | |
| 'menu_icon' => 'dashicons-admin-customizer', | |
| ); | |
| register_post_type(self::POST_TYPE, array( | |
| self::LABEL => __('Gallery', self::BACK_DOMAIN), | |
| self::DESCRIPTION=> __('All lots from all galleries', self::BACK_DOMAIN), | |
| self::LABELS => $labels, | |
| 'supports' => array('title', 'editor', 'thumbnail', 'custom-fields',), | |
| 'taxonomies' => array(self::TAX_TYPE), | |
| 'public' => true, | |
| 'show_ui' => true, | |
| 'show_in_menu' => true, | |
| 'show_in_nav_menus' => true, | |
| 'show_in_admin_bar' => true, | |
| 'menu_position' => 5, | |
| 'can_export' => true, | |
| 'hierarchical' => false, | |
| 'rewrite' => array( | |
| 'slug' => 'gallery/%' . self::TAX_TYPE . '%', | |
| 'with_front' => false, | |
| 'pages' => false, | |
| 'feeds' => false, | |
| 'feed' => false | |
| ), | |
| 'has_archive' => true, | |
| 'query_var' => true, | |
| 'exclude_from_search' => false, | |
| 'publicly_queryable' => true, | |
| 'capability_type' => 'post', | |
| )); | |
| } | |
| public function registerTaxonomy(){ | |
| register_taxonomy_for_object_type( self::TAX_TYPE, self::POST_TYPE); | |
| } | |
| function createGalleryTax() { | |
| register_taxonomy( | |
| self::TAX_TYPE, | |
| self::POST_TYPE, | |
| array( | |
| self::LABELS => array( | |
| 'name' => _x('Case', self::BACK_DOMAIN), | |
| 'menu_name' => __('Cases', self::BACK_DOMAIN), | |
| 'singular_name' => _x('Case', self::BACK_DOMAIN), | |
| 'search_items' => __('Search Case', self::BACK_DOMAIN), | |
| 'popular_items' => __('Popular Case', self::BACK_DOMAIN), | |
| 'all_items' => __('All Case', self::BACK_DOMAIN), | |
| 'parent_item' => __('Parent Case', self::BACK_DOMAIN), | |
| 'parent_item_colon' => __('Parent Case', self::BACK_DOMAIN), | |
| 'edit_item' => __('Edit Case', self::BACK_DOMAIN), | |
| 'update_item' => __('Update Case', self::BACK_DOMAIN), | |
| 'add_new_item' => __('Add New Case', self::BACK_DOMAIN), | |
| 'new_item_name' => __('New Case Number', self::BACK_DOMAIN), | |
| 'separate_items_with_commas' => __('Separate case with commas', self::BACK_DOMAIN), | |
| 'add_or_remove_items' => __('Add or remove cases', self::BACK_DOMAIN), | |
| 'choose_from_most_used' => __('Choose from the most used cases', self::BACK_DOMAIN), | |
| ), | |
| 'rewrite' => array( | |
| 'slug' => 'gallery', | |
| 'with_front' => true, | |
| 'pages' => false, | |
| 'feeds' => false, | |
| 'feed' => false, | |
| ), | |
| 'public' => true, | |
| 'show_in_nav_menus' => false, | |
| 'show_ui' => true, | |
| 'show_tagcloud' => false, | |
| 'update_count_callback' => '_update_post_term_count', | |
| 'hierarchical' => true, | |
| 'show_admin_column' => true, | |
| 'query_var' => true, | |
| ) | |
| ); | |
| } | |
| public function post_for_taxonomy() | |
| { | |
| register_taxonomy_for_object_type(self::TAX_TYPE, self::POST_TYPE); | |
| } | |
| public function true_post_type_messages( $messages ) { | |
| global $post, $post_ID; | |
| $messages[self::POST_TYPE] = array( | |
| 0 => '', | |
| 1 => sprintf( 'Gallery update. <a href="%s">View</a>', esc_url( get_permalink($post_ID) ) ), | |
| 2 => 'Parameter update', | |
| 3 => 'Parameter removed', | |
| 4 => 'Item update', | |
| 5 => isset($_GET['revision']) ? sprintf( 'Item restored from revision: %s', wp_post_revision_title( (int) | |
| $_GET['revision'], false ) ) : false, | |
| 6 => sprintf( 'Item published on the site. <a href="%s">View</a>', esc_url( get_permalink($post_ID) | |
| ) ), | |
| 7 => 'Function saved.', | |
| 8 => sprintf( 'Submitted for review. <a target="_blank" href="%s">View</a>', esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ), | |
| 9 => sprintf( 'Scheduled for publication: <strong>%1$s</strong>. <a target="_blank" href="%2$s">View</a>', | |
| date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ), | |
| 10 => sprintf( 'Draft updated. <a target="_blank" href="%s">View</a>', esc_url( add_query_arg( 'preview', | |
| 'true', get_permalink($post_ID) ) ) ), | |
| ); | |
| return $messages; | |
| } | |
| public function galleryPermalink($permalink, $post) { | |
| if (strpos($permalink, '%' . self::TAX_TYPE . '%') === false) { | |
| return $permalink; | |
| } | |
| $terms = get_the_terms($post, self::TAX_TYPE); | |
| if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) { | |
| $term_slug = array_pop($terms)->slug; | |
| } else { | |
| $term_slug = 'gallery'; | |
| } | |
| return str_replace('%' . self::TAX_TYPE . '%', $term_slug, $permalink); | |
| } | |
| public function unregister() | |
| { | |
| unregister_post_type(self::POST_TYPE); | |
| unregister_taxonomy(self::TAX_TYPE); | |
| } | |
| } | |
| (new NewPostType())->init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment