Skip to content

Instantly share code, notes, and snippets.

@smeric
Last active August 29, 2015 14:08
Show Gist options
  • Save smeric/523c54a16f2ed1870a60 to your computer and use it in GitHub Desktop.
Save smeric/523c54a16f2ed1870a60 to your computer and use it in GitHub Desktop.
Register "item" custom taxonomy for woocommerce products
<?php
/**
* Woocommerce "item" taxonomy
*
* This plugin is a template to add a custom taxonomy to products in woocommerce.
* One should search/replace every "Item" occurence with the taxonomy name.
*
* @link https://gist.github.com/smeric/523c54a16f2ed1870a60
* @since 1.0.0
* @package woocommerce_custom_taxonomy_product_item
*
* @wordpress-plugin
* Plugin Name: Woocommerce "item" taxonomy
* Plugin URI: https://gist.github.com/smeric/523c54a16f2ed1870a60
* Description: This plugin is a template to add a custom taxonomy to products in woocommerce. One should search/replace every "Item" occurence with the taxonomy name.
* Version: 1.0.0
* Author: Sébastien Méric <[email protected]>
* Author URI: http://www.sebastien-meric.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: woocommerce_custom_taxonomy_product_item
* Domain Path: /languages
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Define the internationalization functionality.
*
* Loads and defines the internationalization files for this plugin
* so that its ready for translation.
*
* @since 1.0.0
* @package woocommerce_custom_taxonomy_product_item
* @author Sébastien Méric <[email protected]>
**/
add_action( 'after_setup_theme', 'woocommerce_custom_taxonomy_product_item_after_setup_theme' );
function woocommerce_custom_taxonomy_product_item_after_setup_theme() {
// Load the plugin text domain for translation.
$locale = apply_filters( 'woocommerce_custom_taxonomy_product_item_locale', get_locale(), 'woocommerce_custom_taxonomy_product_item' );
load_textdomain( 'woocommerce_custom_taxonomy_product_item', trailingslashit( WP_LANG_DIR ) . 'woocommerce_custom_taxonomy_product_item/woocommerce_custom_taxonomy_product_item-' . $locale . '.mo' );
load_plugin_textdomain( 'woocommerce_custom_taxonomy_product_item', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Register "item" custom taxonomy for woocommerce products
*
* @since 1.0.0
* @package woocommerce_custom_taxonomy_product_item
* @author Sébastien Méric <[email protected]>
*/
add_action( 'woocommerce_register_taxonomy', 'woocommerce_custom_taxonomy_product_item' );
function woocommerce_custom_taxonomy_product_item() {
register_taxonomy( 'product_item',
apply_filters( 'woocommerce_taxonomy_objects_product_item', array( 'product' ) ),
apply_filters( 'woocommerce_taxonomy_args_product_item', array(
'hierarchical' => true,
'update_count_callback' => '_wc_term_recount',
'label' => __( 'Product Items', 'woocommerce_custom_taxonomy_product_item' ),
'labels' => array(
'name' => __( 'Product Items', 'woocommerce_custom_taxonomy_product_item' ),
'singular_name' => __( 'Product Item', 'woocommerce_custom_taxonomy_product_item' ),
'menu_name' => _x( 'Items', 'Admin menu name', 'woocommerce_custom_taxonomy_product_item' ),
'search_items' => __( 'Search Product Items', 'woocommerce_custom_taxonomy_product_item' ),
'all_items' => __( 'All Product Items', 'woocommerce_custom_taxonomy_product_item' ),
'parent_item' => __( 'Parent Product Item', 'woocommerce_custom_taxonomy_product_item' ),
'parent_item_colon' => __( 'Parent Product Item:', 'woocommerce_custom_taxonomy_product_item' ),
'edit_item' => __( 'Edit Product Item', 'woocommerce_custom_taxonomy_product_item' ),
'update_item' => __( 'Update Product Item', 'woocommerce_custom_taxonomy_product_item' ),
'add_new_item' => __( 'Add New Product Item', 'woocommerce_custom_taxonomy_product_item' ),
'new_item_name' => __( 'New Product Item Name', 'woocommerce_custom_taxonomy_product_item' ),
'separate_items_with_commas' => __( 'Separate Product Item with commas', 'woocommerce_custom_taxonomy_product_item' ),
'search_items' => __( 'Search Product Items', 'woocommerce_custom_taxonomy_product_item' ),
'add_or_remove_items' => __( 'Add or remove Product Items', 'woocommerce_custom_taxonomy_product_item' ),
'choose_from_most_used' => __( 'Choose from the most used Product Items', 'woocommerce_custom_taxonomy_product_item' ),
),
'show_ui' => true,
'query_var' => true,
'capabilities' => array(
'manage_terms' => 'manage_product_terms',
'edit_terms' => 'edit_product_terms',
'delete_terms' => 'delete_product_terms',
'assign_terms' => 'assign_product_terms',
),
'rewrite' => array(
'slug' => empty( $permalinks['item_base'] ) ? _x( 'product-item', 'slug', 'woocommerce_custom_taxonomy_product_item' ) : $permalinks['item_base'],
'with_front' => false,
'hierarchical' => true,
),
) )
);
}
/**
* Display product items after product categories and tags on product page.
*
* @since 1.0.0
* @package woocommerce_custom_taxonomy_product_item
* @author Sébastien Méric <[email protected]>
*/
add_action( 'woocommerce_product_meta_end', 'woocommerce_custom_taxonomy_product_item_display' );
function woocommerce_custom_taxonomy_product_item_display(){
global $post;
$item_count = sizeof( get_the_terms( $post->ID, 'product_item' ) );
echo get_the_term_list( $post->ID, 'product_item', '<span class="itemed_as">' . _n( 'Item:', 'Items:', $item_count, 'woocommerce_custom_taxonomy_product_item' ) . ' ', ', ', '.</span>' );
}
/**
* Init our settings
*
* @since 1.0.0
* @package woocommerce_custom_taxonomy_product_item
* @author Sébastien Méric <[email protected]>
*/
add_action( 'admin_init', 'woocommerce_custom_taxonomy_product_item_initialize_theme_options' );
function woocommerce_custom_taxonomy_product_item_initialize_theme_options() {
// Add our settings
add_settings_field(
'woocommerce_custom_taxonomy_product_item_slug', // id
__( 'Product item base', 'woocommerce_custom_taxonomy_product_item' ), // setting title
'woocommerce_custom_taxonomy_product_item_slug_input', // display callback
'permalink', // settings page
'optional' // settings section
);
// We need to save the options ourselves; settings api does not trigger save for the permalinks page
if ( isset( $_POST['permalink_structure'] ) || isset( $_POST['item_base'] ) ) {
// Item base
$woocommerce_product_item_slug = wc_clean( $_POST['woocommerce_product_item_slug'] );
$permalinks = get_option( 'woocommerce_permalinks' );
if ( ! $permalinks ) {
$permalinks = array();
}
$permalinks['item_base'] = untrailingslashit( $woocommerce_product_item_slug );
update_option( 'woocommerce_permalinks', $permalinks );
}
}
/**
* Show a slug input box on permalinks page.
*
* @since 1.0.0
* @package woocommerce_custom_taxonomy_product_item
* @author Sébastien Méric <[email protected]>
*/
function woocommerce_custom_taxonomy_product_item_slug_input() {
$permalinks = get_option( 'woocommerce_permalinks' );
?>
<input name="woocommerce_product_item_slug" type="text" class="regular-text code" value="<?php if ( isset( $permalinks['item_base'] ) ) echo esc_attr( $permalinks['item_base'] ); ?>" placeholder="<?php echo _x( 'product-item', 'slug', 'woocommerce_custom_taxonomy_product_item' ) ?>" />
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment