Last active
July 23, 2018 05:49
-
-
Save zevilz/e5af370e8eeb3dd0ca550abbf9ebfc64 to your computer and use it in GitHub Desktop.
Register custom post type with custom taxonomy for wordpress. Post type link include taxonomy term.
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 | |
/* | |
* | |
* Register post type "service" | |
* | |
*/ | |
add_action('init', 'register_service', 0); | |
function register_service() { | |
register_post_type('service', [ | |
'label' => null, | |
'labels' => [ | |
'name' => __('Services', 'domain'), | |
'singular_name' => __('Service', 'domain'), | |
'add_new' => __('Add Service', 'domain'), | |
'add_new_item' => __('Add Service', 'domain'), | |
'edit_item' => __('Edit Service', 'domain'), | |
'new_item' => __('New Service', 'domain'), | |
'view_item' => __('View Service', 'domain'), | |
'search_items' => __('Find Services', 'domain'), | |
'not_found' => __('Services not found', 'domain'), | |
'not_found_in_trash' => __('Services not found in trash', 'domain'), | |
'parent_item_colon' => __('Parent Service', 'domain'), | |
'menu_name' => __('Services', 'domain'), | |
'all_items' => __('All Services', 'domain'), | |
'name_admin_bar' => __('Service', 'domain'), | |
'featured_image' => __('Service Thumbnail', 'domain'), | |
'insert_into_item' => __('Insert into Service', 'domain'), | |
'attributes' => __('Service attributes', 'domain'), | |
'uploaded_to_this_item' => __('Uploaded to this Service', 'domain'), | |
], | |
'menu_position' => 20, | |
'menu_icon' => 'dashicons-portfolio', | |
'description' => '', | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_rest' => false, | |
'rest_base' => '', | |
'show_in_menu' => true, | |
'exclude_from_search' => false, | |
'capability_type' => 'post', | |
'map_meta_cap' => true, | |
'hierarchical' => false, | |
'rewrite' => [ | |
'slug' => 'services/%services-cat%', | |
'with_front' => false, | |
'pages' => true, | |
'feeds' => false, | |
'feed' => false | |
], | |
'has_archive' => 'services', | |
'query_var' => true, | |
'supports' => ['title', 'editor', 'thumbnail', 'excerpt', 'comments', 'revisions'], | |
'taxonomies' => ['service-cat'], | |
]); | |
} | |
/* | |
* | |
* Register post type "service" messages | |
* | |
*/ | |
add_filter('post_updated_messages', 'custom_post_updated_messages'); | |
function custom_post_updated_messages($messages) { | |
global $post; | |
$messages['service'] = [ | |
0 => '', | |
1 => sprintf(__('Service updated. <a href="%s">View Service</a>', 'domain'), esc_url(get_permalink($post->ID))), | |
2 => __('Custom field updated', 'domain'), | |
3 => __('Custom field deleted', 'domain'), | |
4 => __('Service updated', 'domain'), | |
5 => isset($_GET['revision']) ? sprintf(__('Service restored from %s', 'domain'), wp_post_revision_title((int)$_GET['revision'], false)) : false, | |
6 => sprintf(__('Service published. <a href="%s">View Service</a>', 'domain'), esc_url(get_permalink($post->ID))), | |
7 => __('Service saved', 'domain'), | |
8 => sprintf(__('Service saved. <a target="_blank" href="%s">Preview Service</a>', 'domain'), esc_url(add_query_arg('preview', 'true', get_permalink($post->ID)))), | |
9 => sprintf(__('Service planned to: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Service</a>', 'domain'), | |
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">Preview Service</a>', 'domain'), esc_url(add_query_arg('preview', 'true', get_permalink($post->ID)))), | |
]; | |
return $messages; | |
} | |
/* | |
* | |
* Register taxonomy "services-cat" for post type "service" | |
* | |
*/ | |
add_action('init', 'register_taxonomy_services_cat', 0); | |
function register_taxonomy_services_cat() { | |
register_taxonomy('services-cat', 'service', [ | |
'labels' => [ | |
'name' => __('Services Categories', 'domain'), | |
'singular_name' => __('Category', 'domain'), | |
'search_items' => __('Search Categories', 'domain'), | |
'all_items' => __('All Categories', 'domain'), | |
'parent_item' => __('Parent Category', 'domain'), | |
'parent_item_colon' => __('Parent Category:', 'domain'), | |
'edit_item' => __('Edit Category', 'domain'), | |
'update_item' => __('Update Category', 'domain'), | |
'add_new_item' => __('Add New Category', 'domain'), | |
'new_item_name' => __('New Category Name', 'domain'), | |
'menu_name' => __('Categories', 'domain'), | |
], | |
'hierarchical' => true, | |
'query_var' => true, | |
'rewrite' => [ | |
'hierarchical' => true, | |
'slug' => 'services', | |
'with_front' => false, | |
], | |
'public' => true, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
]); | |
} | |
/* | |
* | |
* Add services-cat term to service link | |
* | |
*/ | |
add_filter('post_type_link', 'services_permalink', 1, 2); | |
function services_permalink($permalink, $post) { | |
if (strpos($permalink, '%services-cat%') === false) { | |
return $permalink; | |
} | |
$terms = get_the_terms($post, 'services-cat'); | |
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) { | |
$term_slug = array_pop($terms)->slug; | |
} | |
else { | |
$term_slug = 'no-cat'; | |
} | |
return str_replace('%services-cat%', $term_slug, $permalink); | |
} | |
/* | |
* | |
* Set default category for post type "service" if it not setted (optional) | |
* | |
*/ | |
add_action('publish_service', 'set_default_category_for_service'); | |
function set_default_category_for_service($post_ID) { | |
global $wpdb; | |
if (!has_term('', 'services-cat', $post_ID)) { | |
$cat_id = 1; // default category ID | |
$cat = [intval($cat_id))]; | |
wp_set_object_terms($post_ID, $cat, 'services-cat'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment