daily pastebin goal
53%
SHARE
TWEET

WordPress - Create New Custom Taxonomy

a guest Jun 15th, 2017 482 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // create a new taxonomy we're calling 'layout'
  2. // if you call the new taxonomy 'format' and you have support
  3. // for post formats enabled the taxonomy will not be visible or work
  4. function custom_post_formats_taxonomies() {
  5.     // Add new taxonomy, make it hierarchical (like categories)
  6.     $labels = array(
  7.         'name'              => _x( 'Layouts', 'taxonomy general name', 'textdomain' ),
  8.         'singular_name'     => _x( 'Layout', 'taxonomy singular name', 'textdomain' ),
  9.         'search_items'      => __( 'Search Layouts', 'textdomain' ),
  10.         'all_items'         => __( 'All Layouts', 'textdomain' ),
  11.         'parent_item'       => __( 'Parent Layout', 'textdomain' ),
  12.         'parent_item_colon' => __( 'Parent Layout:', 'textdomain' ),
  13.         'edit_item'         => __( 'Edit Layout', 'textdomain' ),
  14.         'update_item'       => __( 'Update Layout', 'textdomain' ),
  15.         'add_new_item'      => __( 'Add New Layout', 'textdomain' ),
  16.         'new_item_name'     => __( 'New Layout Name', 'textdomain' ),
  17.         'menu_name'         => __( 'Format', 'textdomain' ),
  18.     );
  19.  
  20.     $args = array(
  21.         'hierarchical'      => true,
  22.         'labels'            => $labels,
  23.         'show_ui'           => true,
  24.         'show_admin_column' => true,
  25.         'query_var'         => true,
  26.         'rewrite'           => array( 'slug' => 'layout' ),
  27.         'capabilities' => array(
  28.             'manage_terms' => '',
  29.             'edit_terms' => '',
  30.             'delete_terms' => '',
  31.             'assign_terms' => 'edit_posts'
  32.         ),
  33.         'public' => true,
  34.         'show_in_nav_menus' => true,
  35.         'show_tagcloud' => true,
  36.     );
  37.     register_taxonomy( 'layout', array( 'post' ), $args ); // our new 'format' taxonomy
  38. }
  39.  
  40. // hook into the init action and call custom_post_formats_taxonomies when it fires
  41. add_action( 'init', 'custom_post_formats_taxonomies', 0 );
  42.  
  43. // programmatically create a few format terms
  44. function example_insert_default_layout() { // later we'll define this as our default, so all posts have to have at least one format
  45.     wp_insert_term(
  46.         'Default',
  47.         'layout',
  48.         array(
  49.           'description' => '',
  50.           'slug'        => 'default'
  51.         )
  52.     );
  53. }
  54. add_action( 'init', 'example_insert_default_layout' );
  55.  
  56. // repeat the following 11 lines for each format you want
  57. function example_insert_map_layout() {
  58.     wp_insert_term(
  59.         'Map', // change this to
  60.         'layout',
  61.         array(
  62.           'description' => 'Adds a large map to the top of your post.',
  63.           'slug'        => 'map'
  64.         )
  65.     );
  66. }
  67. add_action( 'init', 'example_insert_map_layout' );
  68.  
  69. // make sure there's a default Format type and that it's chosen if they didn't choose one
  70. function moseyhome_default_layout_term( $post_id, $post ) {
  71.     if ( 'publish' === $post->post_status ) {
  72.         $defaults = array(
  73.             'layout' => 'default' // change 'default' to whatever term slug you created above that you want to be the default
  74.             );
  75.         $taxonomies = get_object_taxonomies( $post->post_type );
  76.         foreach ( (array) $taxonomies as $taxonomy ) {
  77.             $terms = wp_get_post_terms( $post_id, $taxonomy );
  78.             if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
  79.                 wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
  80.             }
  81.         }
  82.     }
  83. }
  84. add_action( 'save_post', 'moseyhome_default_layout_term', 100, 2 );
  85.  
  86. // replace checkboxes for the format taxonomy with radio buttons and a custom meta box
  87. function wpse_139269_term_radio_checklist( $args ) {
  88.     if ( ! empty( $args['taxonomy'] ) && $args['taxonomy'] === 'layout' ) {
  89.         if ( empty( $args['walker'] ) || is_a( $args['walker'], 'Walker' ) ) { // Don't override 3rd party walkers.
  90.             if ( ! class_exists( 'WPSE_139269_Walker_Category_Radio_Checklist' ) ) {
  91.                 class WPSE_139269_Walker_Category_Radio_Checklist extends Walker_Category_Checklist {
  92.                     function walk( $elements, $max_depth, $args = array() ) {
  93.                         $output = parent::walk( $elements, $max_depth, $args );
  94.                         $output = str_replace(
  95.                             array( 'type="checkbox"', "type='checkbox'" ),
  96.                             array( 'type="radio"', "type='radio'" ),
  97.                             $output
  98.                         );
  99.                         return $output;
  100.                     }
  101.                 }
  102.             }
  103.             $args['walker'] = new WPSE_139269_Walker_Category_Radio_Checklist;
  104.         }
  105.     }
  106.     return $args;
  107. }
  108.  
  109. add_filter( 'wp_terms_checklist_args', 'wpse_139269_term_radio_checklist' );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top