Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@neilgee
Last active November 7, 2021 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neilgee/10787308 to your computer and use it in GitHub Desktop.
Save neilgee/10787308 to your computer and use it in GitHub Desktop.
WordPress Custom Taxonomy Plugin Example
<?php
// Add custom taxonomies
/*
Plugin Name: WP Beaches Taxonomies
Plugin URI: http://wpbeaches.com/
Description: WP Beaches Custom Taxonomies
Author: Neil Gowran
Version: 1.1.0
Author URI: http://wpbeaches.com
*/
/*Further reference - https://codex.wordpress.org/Function_Reference/register_taxonomy
This example registers the term 'portfolio_category' as the example term in the Category section and 'portfolio_tag' in the Tag example, you need to replace these with the actual term.
The use of _x in certain vales in the array are for translation issues - https://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context
*/
add_action( 'init', 'themeprefix_taxonomies');
function themeprefix_taxonomies() {
// Hierarchal Taxonomy aka Category style - this example uses portfolio
$labels = array(
'name' => _x( 'Portfolio Category', 'taxonomy general name' ),
'singular_name' => _x( 'Portfolio Category', 'taxonomy singular name' ),
'search_items' => __( 'Search in Portfolios Categories' ),
'all_items' => __( 'All Portfolios Categories' ),
'most_used_items' => null,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Portfolio Category' ),
'update_item' => __( 'Update Portfolio Category' ),
'add_new_item' => __( 'Add new Portfolio Category' ),
'new_item_name' => __( 'New Portfolio Category' ),
'menu_name' => __( 'Portfolio Category' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_tagcloud' => true,
'hierarchical' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'portfolio-category' ),
);
register_taxonomy( 'portfolio_category', array( 'portfolio', 'menu' ), $args );//add in your CPTS that the Taxonomy is applicable to
// Flat Taxonomy aka Tag like - this example uses portfolio
$labels = array(
'name' => _x( 'Portfolio Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Portfolio Tag', 'taxonomy singular name' ),
'search_items' => __( 'Search in Portfolios Tags' ),
'all_items' => __( 'All Portfolios Tags' ),
'most_used_items' => null,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Portfolio Tag' ),
'update_item' => __( 'Update Portfolio Tag' ),
'add_new_item' => __( 'Add new Portfolio Tag' ),
'new_item_name' => __( 'New Portfolio Tags' ),
'separate_items_with_commas' => __( 'Separate Portfolios Tags with commas' ),
'choose_from_most_used' => __( 'Choose from the most used Portfolios Tags' ),
'not-found' => __( 'No Portfolios Tags found'),
'menu_name' => __( 'Portfolios Tags' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_tagcloud' => true,
'hierarchical' => false,
'show_admin_column' => true,
'query_var' => true,
'update_count_callback' => '_update_post_term_count',
'rewrite' => array( 'slug' => 'portfolio-tag' ),
);
register_taxonomy( 'portfolio_tags', array( 'portfolio', 'menu' ), $args );//add in your CPTS that the Taxonomy is applicable to this example links it to regular posts and a 'menu' custom post type
}
//Flush the permalinks - ref - https://codex.wordpress.org/Function_Reference/register_post_type#Flushing_Rewrite_on_Activation
function prefix_my_rewrite_flush() {
// First, we "add" the custom taxonomies via the above written function.
// Then we flush the permalinks when the plugin is activated so the new taxonomy archives are readily available.
themeprefix_taxonomies();
// ATTENTION: This is *only* done during plugin activation hook in this example!
// You should *NEVER EVER* do this on every page load!!
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'prefix_my_rewrite_flush' );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment