How to Add Custom Taxonomy To Woocomerce Plugin - woocommerce

I am trying to add a Custom Taxonomy to Woocommerce by targeting the product post type in woocommerce. I used following code and add them into function.php I am not getting any error message but the tax is not showing in the Woocommerce as well.
Can you please let me know how I can do this or what I am doing wrong here?
<?php
// Register Custom Taxonomy
function custom_taxonomy_Item() {
$labels = array(
'name' => 'Items',
'singular_name' => 'Item',
'menu_name' => 'Item',
'all_items' => 'All Items',
'parent_item' => 'Parent Item',
'parent_item_colon' => 'Parent Item:',
'new_item_name' => 'New Item Name',
'add_new_item' => 'Add New Item',
'edit_item' => 'Edit Item',
'update_item' => 'Update Item',
'separate_items_with_commas' => 'Separate Item with commas',
'search_items' => 'Search Items',
'add_or_remove_items' => 'Add or remove Items',
'choose_from_most_used' => 'Choose from the most used Items',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'item', 'product', $args );
}
?>

This php function is not being called, at least in this code. Make sure you are calling your custom_taxonomy_item and hook into init;
add_action( 'init', 'custom_taxonomy_item', 0 );
And place this above/below your code.

Related

Custom Taxonomy added, still showing the category

I have made a new plugin for my WordPress site, which has the capability of creating a new post type called products and on this plug in I have added a custom taxonomy called product types. Everything is fine but my Wordpress admin panel custom post type 'product' still showing the categories option, which I don't want. That's my plug in code,
<?php function FLChildTheme_custom_post_type (){
$labels = array(
'name' => 'Products',
'singular_name' => 'products',
'add_new' => 'Add Products',
'all_items' => 'All items',
'add_new_item' => 'Add item',
'edit_item' => 'Edit Item',
'new_item' => 'New Item',
'view_item' => 'View Products',
'search_item' => 'Search Product',
'not_found' => 'No Products Found',
'not_found_in_trash' => 'No product in trash',
'parent_item_colon' => 'Parent Item',
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail',
'revisions',
),
'taxonomies' => array('category','post_tag'),
'menu_position' => 5,
'exclude_from_search' => false,
);
register_post_type('products', $args);
}
add_action ('init', 'FLChildTheme_custom_post_type');
/* Custom post type End*/
/* Add custom taxonomies */
function FLChildTheme_add_custom_taxonomies() {
// Add new "Locations" taxonomy to Posts
register_taxonomy('Producttypes', 'products', array(
// Hierarchical taxonomy (like categories)
'hierarchical' => true,
// This array of options controls the labels displayed in the WordPress Admin UI
'labels' => array(
'name' => _x( 'Product Types', 'taxonomy general name' ),
'singular_name' => _x( 'Product Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Product Types' ),
'all_items' => __( 'All Product Types' ),
'parent_item' => __( 'Parent Product Types' ),
'parent_item_colon' => __( 'Parent Product Types:' ),
'edit_item' => __( 'Edit Product Types' ),
'update_item' => __( 'Update Product Types' ),
'add_new_item' => __( 'Add New Product Types' ),
'new_item_name' => __( 'New Product Type Name' ),
'menu_name' => __( 'Product Types' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'Producttypes', // This controls the base slug that will display before each term
'with_front' => false, // Don't display the category base before "/locations/"
'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
),
));
}
add_action( 'init', 'FLChildTheme_add_custom_taxonomies', 0 );
?>

Woocommerce taxonomy

i made custom taxonomy for woocomerce products, products will be books and ecery book have his own writter.
<pre>function custom_taxonomy_writter() {
$labels = array(
'name' => 'Writters',
'singular_name' => 'Writter',
'menu_name' => 'Writter',
'all_items' => 'All writters',
'parent_item' => 'Parent writter',
'parent_item_colon' => 'Parent writter:',
'new_item_name' => 'New writter name',
'add_new_item' => 'Add new writter',
'edit_item' => 'Edit writter',
'update_item' => 'Update writter',
'separate_items_with_commas' => 'Separate writter with commas',
'search_items' => 'Search writters',
'add_or_remove_items' => 'Add or remove writters',
'choose_from_most_used' => 'Choose from the most used writters',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy('writter', 'product', $args);
}
add_action('init', 'custom_taxonomy_writter', 0);</pre>
Screenshoot from wp admin:
Now I need to display it for example under the product title, and needs to bi link, like tags or categories.
Wooola!
<?php echo get_the_term_list( $post->ID, 'writter', '', ', ' ); ?>
Simple and works!!! And is also linked to author with listing of his books :)

insert taxonomy to a custom post type

I have been created a custom post type and create a taxonomy for it, below is the code which i used to create those:
//create custom post type of jobs
add_action( 'init', 'create_jobs' );
function create_jobs() {
register_post_type( 'jobs',
array(
'labels' => array(
'name' => 'jobs',
'singular_name' => 'Jobs',
'add_new' => 'Add New',
'add_new_item' => 'Add New Jobs',
'edit' => 'Edit',
'edit_item' => 'Edit Jobs',
'new_item' => 'New Jobs',
'view' => 'View',
'view_item' => 'View Jobs',
'search_items' => 'Search Jobs',
'not_found' => 'No Jobs found',
'not_found_in_trash' => 'No Jobs found in Trash',
'parent' => 'Parent Jobs'
),
'public' => true,
'menu_position' => 15,
'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
'taxonomies' => array( '' ),
'menu_icon' => 'dashicons-visibility',
'has_archive' => true
)
);
}
//create a taxonomy for jobs
add_action( 'init', 'create_jobstax', 0 );
function create_jobstax() {
register_taxonomy(
'jobs_taxonomy_genre',
'jobs',
array(
'labels' => array(
'name' => 'Create Jobs Genre',
'add_new_item' => 'Add New jobs genre',
'new_item_name' => "New jobs genre"
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true
)
);
}
now what i want is to insert a default taxonomy for that custom post type only, example: i want to insert "latest" and "active" as a default taxonomy for the jobs custom post type, how to achieve that? currently looking for a approach around but seems found nothing to meet my needs. Any ideas, recommendations and suggestions, would love to hear. Very thank you in advance.
Please try this plugin, you don't need to take headache like this. you will get complete separate module for your post type with category and tag
custom post type maker
1) create a post type "jobs" as per your requirement.
2) then create 2 taxonomy with name like 'Job category' & 'Job tags'.
3) when your creating the taxonomy Please select your job post type which is given at the bottom of taxonomy creation page.
finally you will get the complete separate module with following
Customize this code .
add_action('init','create_custom_posts');
function create_custom_posts(){
register_post_type( 'cms_chooser', array(
'labels' => array(
'name' => 'CMS Chooser',
'singular_name' => 'CMS Chooser',
'add_new' => 'Add New',
'add_new_item' => 'Add New CMS',
'edit_item' => 'Edit',
'edit' => 'CMS',
'new_item' => 'New CMS',
'view_item' => 'View Detail',
'search_items' => 'Search in CMS',
'not_found' => 'No CMS found',
'not_found_in_trash' => 'No Posts found in Trash',
'view' => 'View Post'
),
'public' => true,
//'capability_type' => 'post',
'hierarchical' => true,
'exclude_from_search' => false,
'show_in_nav_menus' => false,
'query_var' => true,
'menu_position' => 5,
'supports' => array('title','page-attributes'),
'has_archive' => true,
'taxonomies' => array('cms-category'),
'rewrite' => array('slug' => 'cms', 'with_front' => false )
)
);
register_taxonomy(
'cms-category',
'cms_chooser',
array(
'labels' => array(
'name' => 'Category',
'singular_name' => 'Category',
'search_items' => 'Search Category',
'popular_items' => 'Popular Category',
'all_items' => 'All Category',
'parent_item' => 'Parent Category',
'parent_item_colon' => 'Parent Category',
'edit_item' => 'Edit Category',
'update_item' => 'Update Category',
'add_new_item' => 'Add New Category',
'new_item_name' => 'New Category Name'
),
'hierarchical' => false,
'show_in_nav_menus' => false,
'exclude_from_search' => true,
'public' => false,
'show_ui' => true,
'query_var' => 'cms-category',
'show_tagcloud' => true,
'rewrite' => true
)
);
}
I would have approached it this way.
Create categories "latest","active","xyz","blah-blah", for this custom-post.
On admin side add js to select the required two categories. (Let me know if you need code for this.)

WordPress: How to add some of tags automatically during installation of WordPress theme?

How to add some of tags automatically during installation of WordPress theme?
Put a register_taxonomy() in theme's function.php. I'm copy pasting from WP Codex
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'Writers', 'taxonomy general name' ),
'singular_name' => _x( 'Writer', 'taxonomy singular name' ),
'search_items' => __( 'Search Writers' ),
'popular_items' => __( 'Popular Writers' ),
'all_items' => __( 'All Writers' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Writer' ),
'update_item' => __( 'Update Writer' ),
'add_new_item' => __( 'Add New Writer' ),
'new_item_name' => __( 'New Writer Name' ),
'separate_items_with_commas' => __( 'Separate writers with commas' ),
'add_or_remove_items' => __( 'Add or remove writers' ),
'choose_from_most_used' => __( 'Choose from the most used writers' ),
'not_found' => __( 'No writers found.' ),
'menu_name' => __( 'Writers' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'writer' ),
);
register_taxonomy( 'writer', 'book', $args );
This will not add tax on installation, but on first loading; anyway I think this should be the way (if you install it, you'll load it).
You can use wp_insert_term to add terms programmatically. See the function reference. There is also wp_update_term. These functions work for all types of terms - categories and tags as well as custom taxonomies.
You will need to put these in a function in your functions.php file and then add it to the init action.
I haven't tested this code, but it should work:
function add_tags() {
wp_insert_term(
'Butts', // the tag/term name
'tags', // the taxonomy name
array(
'description' => 'Posts about Butts',
'slug' => 'butts'
));
add_action( 'init', 'add_tags' );

remove custom taxonomy metabox form custom post type

I have created a custom post type 'hotel' and custom 'taxonomy' so when administrator creates a new hotel and saves it it related custom taxonomy automatically get created but I don't want to show custom metabox in the admin side hotel edit page so for that I used WordPress function but nothing happen.
My custom post code
$Hotel_labels = array(
'name' => _x('Hotels', 'post type general name'),
'singular_name' => _x('Hotel', 'post type singular name'),
'add_new' => _x('Add New', 'Hotel'),
'add_new_item' => __('Add Hotel'),
'edit_item' => __('Edit Hotel'),
'new_item' => __('New Hotel'),
'all_items' => __('All Hotels'),
'view_item' => __('View Hotel'),
'search_items' => __('Search Hotel'),
'not_found' => __('No Hotel found'),
'not_found_in_trash' => __('No Hotel found in Trash'),
'parent_item_colon' => '',
'menu_name' => __('Hotel'),
);
$Hotel_args = array(
'labels' => $Hotel_labels,
'public' => true,
'rewrite' => array('slug' => 'Hotel'),
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 100,
'supports' => array( 'title', 'editor', 'author', 'thumbnail' ),
'taxonomies' => array('hotel_facilities','package_hotel','post_tag')
);
register_post_type('Hotel',$Hotel_args);
Custom taxonomy code
$Package_labels = array(
'name' => _x( 'Package Hotels', 'taxonomy general name' ),
'singular_name' => _x( 'hotel', 'taxonomy singular name' ),
'search_items' => __( 'Search hotels' ),
'popular_items' => __( 'Popular hotels' ),
'all_items' => __( 'All hotels' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit hotel' ),
'update_item' => __( 'Update hotel' ),
'add_new_item' => __( 'Add New hotel' ),
'new_item_name' => __( 'New hotel Name' ),
'separate_items_with_commas' => __( 'Separate hotels with commas' ),
'add_or_remove_items' => __( 'Add or remove hotels' ),
'choose_from_most_used' => __( 'Choose from the most used hotels' ),
'menu_name' => __( 'Package Hotels' ),
);
register_taxonomy('package_hotel','package',array(
'hierarchical' => false,
'labels' => $Package_labels,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'show_in_nav_menus' => false,
'rewrite' => array( 'slug' => 'hotels' ),
));
code to remove custom taxonomy metabox form custom post type hotel page
function my_remove_meta_boxes() {
remove_meta_box('tagsdiv_hotels', 'Hotel', 'side');
}
add_action( 'admin_menu', 'my_remove_meta_boxes' );
Since WordPress 3.8 version there is a meta_box_cb argument available. Just change this argument to false in your arguments definition so
$args = array(
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'meta_box_cb' => false,
);
This will leave a sub-menu item under your custom post type but will remove a meta box on post edit page.
change meta box id enter code here
function my_remove_meta_boxes() { remove_meta_box('package_hotel',
'Hotel', 'side'); } add_action( 'admin_menu', 'my_remove_meta_boxes'
);
Change register_taxonomy() : you have kept
'show_ui' => true,
change it to
'show_ui' => false,
this argument determines whether or not a user interface should be displayed to manage the taxonomy. As you don't want it, you have to set it to false.
Ok now the example given in Wordpress.org is like this
`remove_meta_box($custom_taxonomy_slug.'div', $custom_post_type, 'side' );`
// $custom_taxonomy_slug is the slug of your taxonomy, e.g. 'genre' )
// $custom_post_type is the "slug" of your post type, e.g. 'movies' )
which further directs to write syntax for metabox attribute in the function like this
remove_meta_box( 'submitdiv', 'custom_post_id', 'side' );
and in my case the syntax for custom metabox attribute which worked is like this
remove_meta_box('tagsdiv-package_hotel', 'hotel', 'side');
so instead of appending div at the end append tagsdiv_ in the begnning eg:- tagsdiv_$your_custom_taxonomy_id

Resources