Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save developer-anuragsingh/1208e744b7d180e0d1a7 to your computer and use it in GitHub Desktop.

Select an option

Save developer-anuragsingh/1208e744b7d180e0d1a7 to your computer and use it in GitHub Desktop.
<?php
// Add new custom hierarchical taxonomy with oops ways
add_action( 'init', 'new_hierarchical_taxonomy_type_for_cpt' );
function new_hierarchical_taxonomy_type_for_cpt() {
$post_type = 'job-opening';
$all_taxonomy = array('Department','Nature of Job', 'Qualification', 'Age', 'Location', 'Experience', 'CTC');
foreach ($all_taxonomy as $single) {
$single = ucwords(strtolower(preg_replace('/\s+/', ' ', $single) ));
$last_character = substr($single, -1);
if ($last_character === 'y') {
$plural = substr_replace($single, 'ies', -1);
}
else {
$plural = $single.'s'; // add 's' to convert singular name to plural
}
$tax_slug = $post_type.'_'.strtolower(str_replace(' ', '_', $single)); // add a 'post_type' as prefix followed by a '_'
$opts['hierarchical'] = TRUE;
//$opts['meta_box_cb'] = '';
$opts['public'] = TRUE;
$opts['query_var'] = $tax_slug;
$opts['show_admin_column'] = FALSE;
$opts['show_in_nav_menus'] = TRUE;
$opts['show_tag_cloud'] = TRUE;
$opts['show_ui'] = TRUE;
$opts['sort'] = '';
//$opts['update_count_callback'] = '';
$opts['capabilities']['assign_terms'] = 'edit_posts';
$opts['capabilities']['delete_terms'] = 'manage_categories';
$opts['capabilities']['edit_terms'] = 'manage_categories';
$opts['capabilities']['manage_terms'] = 'manage_categories';
$opts['labels']['add_new_item'] = __( "Add New $single", 'plugin-name' );
$opts['labels']['add_or_remove_items'] = __( "Add or remove {$plural}", 'plugin-name' );
$opts['labels']['all_items'] = __( $plural, 'plugin-name' );
$opts['labels']['choose_from_most_used'] = __( "Choose from most used {$plural}", 'plugin-name' );
$opts['labels']['edit_item'] = __( "Edit {$single}" , 'plugin-name');
$opts['labels']['menu_name'] = __( $plural, 'plugin-name' );
$opts['labels']['name'] = __( $plural, 'plugin-name' );
$opts['labels']['new_item_name'] = __( "New {$single} Name", 'plugin-name' );
$opts['labels']['not_found'] = __( "No {$plural} Found", 'plugin-name' );
$opts['labels']['parent_item'] = __( "Parent {$single}", 'plugin-name' );
$opts['labels']['parent_item_colon'] = __( "Parent {$single}:", 'plugin-name' );
$opts['labels']['popular_items'] = __( "Popular {$plural}", 'plugin-name' );
$opts['labels']['search_items'] = __( "Search {$plural}", 'plugin-name' );
$opts['labels']['separate_items_with_commas'] = __( "Separate {$plural} with commas", 'plugin-name' );
$opts['labels']['singular_name'] = __( $single, 'plugin-name' );
$opts['labels']['update_item'] = __( "Update {$single}", 'plugin-name' );
$opts['labels']['view_item'] = __( "View {$single}", 'plugin-name' );
$opts['rewrite']['ep_mask'] = EP_NONE;
$opts['rewrite']['hierarchical'] = FALSE;
$opts['rewrite']['slug'] = __( $tax_slug, 'plugin-name' );
$opts['rewrite']['with_front'] = FALSE;
register_taxonomy( $tax_slug, $post_type, $opts );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment