Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jotapepinheiro/5160221 to your computer and use it in GitHub Desktop.
Save jotapepinheiro/5160221 to your computer and use it in GitHub Desktop.
<table class="form-table">
<tr class="form-field">
<th><label for="category_color">Category color</label></th>
<td>
<input id="category_color" type="text" name="category_color" value="<?php echo $current_color ?>" />
<span class="description">
Category Color.</span>
</td>
</tr>
</table>
<?php
/*
Plugin Name: JAB: Products post type
Description: Plugin for products custom post type
Author: Luis Abarca
Author Uri: http://luisabarca.com
Plugin Uri: http://justoalblanco.com
*/
/*
* Template functions
*/
function the_category_color($term_id = null)
{
if ( is_null($term_id) ) {
global $post;
$terms = wp_get_object_terms($post->ID, Product::TAXONOMY_NAME);
if ( !empty($terms) ) {
foreach ($terms as $term) {
$term_id = $term->term_id;
}
}
}
the_product_category_field($term_id, 'category_color');
}
function the_product_category_field($term_id, $field)
{
echo get_product_category_field($term_id, $field);
}
function get_product_category_field($term_id, $field)
{
return Product::get_taxonomy_field($term_id, $field);
}
// end template functions
$product = Product::instance();
/**
* create tables for taxonomies
* Inspired by http://shibashake.com/wordpress-theme/add-term-or-taxonomy-meta-data
*/
function _create_metadata_table($type)
{
global $wpdb;
$table_name = $wpdb->prefix . $type . 'meta';
if (!empty ($wpdb->charset)) {
$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
}
if (!empty ($wpdb->collate)) {
$charset_collate .= " COLLATE {$wpdb->collate}";
}
$sql = "CREATE TABLE IF NOT EXISTS {$table_name} (
meta_id bigint(20) NOT NULL AUTO_INCREMENT,
{$type}_id bigint(20) NOT NULL default 0,
meta_key varchar(255) DEFAULT NULL,
meta_value longtext DEFAULT NULL,
UNIQUE KEY meta_id (meta_id)
) {$charset_collate};";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
class Product {
const POST_TYPE_NAME = 'products';
const POST_TYPE_REWRITE = 'products';
const PLURAL_NAME = 'Products';
const SINGULAR_NAME = 'Product';
const TAXONOMY_NAME = 'product-category';
const TAXONOMY_SLUG_NAME = 'product_category';
private static $instance;
/***************************************************************************
* Static functions
**************************************************************************/
public static function instance () {
if ( ! isset( self::$instance ) ) {
$class_name = __CLASS__;
self::$instance = new $class_name;
}
return self::$instance;
}
private function __construct()
{
//if ( function_exists('add_term_thumbnails_support') ) add_term_thumbnails_support(self::TAXONOMY_NAME);
global $wpdb;
$wpdb->{self::TAXONOMY_SLUG_NAME . 'meta'} = $wpdb->prefix . self::TAXONOMY_SLUG_NAME . 'meta';
add_action('init', array($this, 'register_custom_types') );
add_filter('post_type_link', array($this, 'custom_permalinks'), 1, 3);
register_activation_hook(__FILE__, array($this, 'activate') );
register_deactivation_hook(__FILE__, array($this, 'deactivate') );
// add custom fields to taxonomy form
add_action( self::TAXONOMY_NAME . '_edit_form', array($this, 'taxonomy_extra_fields') );
// Save the custom field to table
add_action( 'edited_' . self::TAXONOMY_NAME, array($this, 'taxonomy_save_extra_fields'), 10, 1 );
}
// }}}
// {{{
function taxonomy_save_extra_fields( $term_id )
{
// Form field with the color value
$color = trim($_POST['category_color']);
update_metadata(self::TAXONOMY_SLUG_NAME, $term_id, 'category_color', $color);
}
// }}}
// {{{
/**
*
*/
function taxonomy_extra_fields()
{
$current_color = esc_attr(self::get_taxonomy_field($_GET['tag_ID'], 'category_color'));
include 'category-color-field.php';
}
// }}}
// {{{
public static function get_taxonomy_field($term_id, $field, $single = true)
{
return get_metadata(self::TAXONOMY_SLUG_NAME, $term_id, $field, $single);
}
// }}}
// {{{
/*
*
*/
public function custom_permalinks($url, $post = null, $leavename = false)
{
// products only
if ($post->post_type != self::POST_TYPE_NAME) {
return $url;
}
$post_id = $post->ID;
$taxonomy = self::TAXONOMY_NAME;
$taxonomy_tag = '%' . $taxonomy . '%';
// Check if exists the tag
if (strpos($taxonomy_tag, $url) < 0) {
return $url;
}
// Get the terms
$terms = wp_get_post_terms($post_id, $taxonomy);
if (is_array($terms) && sizeof($terms) > 0) {
$category = $terms[0];
}
// replace taxonomy tag with the term slug: /products/dvd/productname
return str_replace($taxonomy_tag, $category->slug, $url);
}
/*
*
*
*/
public function register_custom_types()
{
global $wp_rewrite;
global $wp_query;
register_post_type(self::POST_TYPE_NAME, array(
'label' => self::PLURAL_NAME,
'description' => '',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'hierarchical' => true,
'rewrite' => array('slug' => self::POST_TYPE_REWRITE),
'query_var' => true,
'has_archive' => true,
'menu_position' => 6,
'supports' => array( 'title', 'editor', 'excerpt', 'trackbacks', 'revisions', 'thumbnail', 'author' ),
'labels' => array(
'name' => self::PLURAL_NAME,
'singular_name' => self::SINGULAR_NAME,
'menu_name' => self::PLURAL_NAME,
'add_new' => 'Add product',
'add_new_item' => 'Add New product',
'edit' => 'Edit',
'edit_item' => 'Edit product',
'new_item' => 'New product',
'view' => 'View product',
'view_item' => 'View product',
'search_items' => 'Search Products',
'not_found' => 'No Products Found',
'not_found_in_trash' => 'No Products Found in Trash',
'parent' => 'Parent product')
)
);
register_taxonomy(self::TAXONOMY_NAME, self::POST_TYPE_NAME, array(
'hierarchical' => true,
'label' => 'Product Categories',
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => 'products'),
'singular_label' => 'Product Category')
);
$wp_rewrite->extra_permastructs[self::POST_TYPE_NAME][0] = '/'. self::POST_TYPE_REWRITE . '/%' . self::TAXONOMY_NAME . '%/%' . self::POST_TYPE_NAME . '%';
}
/**
*
*/
public function activate()
{
self::register_custom_types();
flush_rewrite_rules();
_create_metadata_table(self::TAXONOMY_SLUG_NAME);
}
// }}}
// {{{
/**
*
*/
public function deactivate()
{
flush_rewrite_rules();
}
public static function get_parent_categories($hide_empty = 0)
{
$args = array(
'order' => 'name',
'parent' => 0,
'hide_empty' => $hide_empty
);
return get_terms(self::TAXONOMY_NAME, $args );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment