Skip to content

Instantly share code, notes, and snippets.

@hirozed
Last active February 1, 2022 21:25
Show Gist options
  • Save hirozed/a8fbe6a728bfaada5a8457c51beec89b to your computer and use it in GitHub Desktop.
Save hirozed/a8fbe6a728bfaada5a8457c51beec89b to your computer and use it in GitHub Desktop.
WordPress Taxonomy
<?php
/**
* Data and call to register the taxonomy.
*
* @param array $tax Data for the taxonomy.
*/
function build_taxonomy( $tax ) {
$labels = array(
'name' => $tax['plural'],
'singular_name' => $tax['singular'],
'search_items' => __( 'Search ', '[localization]' ) . $tax['plural'],
'popular_items' => __( 'Popular ', '[localization]' ) . $tax['plural'],
'all_items' => __( 'All ', '[localization]' ) . $tax['plural'],
'edit_item' => __( 'Edit ', '[localization]' ) . $tax['singular'],
'update_item' => __( 'Update ', '[localization]' ) . $tax['singular'],
'add_new_item' => __( 'Add New ', '[localization]' ) . $tax['singular'],
'new_item_name' => __( 'New ', '[localization]' ) . $tax['singular'] . ' Name',
'separate_items_with_commas' => __( 'Separate ', '[localization]' ) . strtolower( $tax['singular'] ) . __( 's with commas', '[localization]' ),
'add_or_remove_items' => __( 'Add or remove ', '[localization]' ) . strtolower( $tax['plural'] ),
'choose_from_most_used' => __( 'Choose from the most used ', '[localization]' ) . strtolower( $tax['singular'] ),
'not_found' => __( 'No ', '[localization]' ) . strtolower( $tax['singular'] ) . __( 's found.', '[localization]' ),
'view_item' => __( 'View ', '[localization]' ) . $tax['singular'],
'parent_item' => __( 'Parent ', '[localization]' ) . $tax['singular'],
);
$args = array(
'public' => true,
'show_ui' => $tax['show_ui'],
'hierarchical' => $tax['hierarchical'],
'show_admin_column' => $tax['show_admin_column'],
'rewrite' => array( 'hierarchical' => false ),
'labels' => $labels,
);
register_taxonomy( $tax['slug'], '[CPT]', $args );
}
/**
* Calls build_taxonomy for each tax needed.
*/
function action_tax() {
$taxes = array(
array(
'slug' => '[TAX]',
'singular' => __( '[Singular Label]', '[localization]' ),
'plural' => __( '[Plural Label]', '[localization]' ),
'hierarchical' => true,
'show_admin_column' => true,
'show_ui' => true,
'localization' => '[localization]',
),
);
foreach ( $taxes as $tax ) {
build_taxonomy( $tax );
}
}
add_action( 'init', __NAMESPACE__ . '\\action_tax' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment