Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espellcaste/6276905 to your computer and use it in GitHub Desktop.
Save espellcaste/6276905 to your computer and use it in GitHub Desktop.
<?php
/*
* Plugin Name: Category Image Field
* Plugin URI: http://claudiosmweb.com/
* Description: Adds image field in category description.
* Version: 0.1
* Author: Claudio Sanches
* Author URI: http://claudiosmweb.com/
* License: GPLv2 or later
*/
class Category_Image_Field {
public function __construct() {
add_action( 'edit_category_form_fields', array( $this, 'register_fields' ) );
add_action( 'edited_category', array( $this, 'save_fileds' ) );
if ( isset( $_GET['taxonomy'] ) && $_GET['taxonomy'] == 'category' ) {
add_action( 'admin_init', array( &$this, 'scripts' ) );
}
}
/**
* Load options scripts.
*
* @return void
*/
function scripts() {
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'media-upload' );
wp_enqueue_script( 'thickbox' );
wp_enqueue_style( 'thickbox' );
}
/**
* Register fields.
*
* @param object $term Object with term data.
*
* @return string Form new fields.
*/
public function register_fields( $term ) {
$category_meta = get_option( 'category_' . $term->term_id );
$default_image = $category_meta['image'] ? $category_meta['image'] : '';
$default_url = $category_meta['url'] ? $category_meta['url'] : '';
$html = '<tr>';
$html .= '<th valign="top" scope="row">';
$html .= sprintf( '<label for="category-meta-image">%s</label>', __( 'Imagem', 'cif' ) );
$html .= '</th>';
$html .= '<td>';
$html .= sprintf( '<input type="text" id="category-meta-image" name="category_meta[image]" value="%s" class="regular-text" />', $default_image );
$html .= sprintf( ' <input class="button" id="category-meta-image-button" type="button" value="%s" />', __( 'Selecionar arquivo', 'cif' ) );
$html .= '<script type="text/javascript">';
$html .= 'jQuery(document).ready(function($) {';
$html .= '$("#category-meta-image-button").click(function() {';
$html .= 'uploadID = $(this).prev("input");';
$html .= 'formfield = $("category-meta-image").attr("name");';
$html .= 'tb_show("", "media-upload.php?post_id=&amp;type=image&amp;TB_iframe=true");';
$html .= 'return false;';
$html .= '});';
$html .= 'window.send_to_editor = function(html) {';
$html .= 'imgurl = $("img", html).attr("src");';
$html .= 'uploadID.val(imgurl);';
$html .= 'tb_remove();';
$html .= '}';
$html .= '});';
$html .= '</script>';
$html .= '</td>';
$html .= '</tr>';
$html .= '<tr>';
$html .= '<th valign="top" scope="row">';
$html .= sprintf( '<label for="category-meta-url">%s</label>', __( 'URL', 'cif' ) );
$html .= '</th>';
$html .= '<td>';
$html .= sprintf( '<input type="text" id="category-meta-url" name="category_meta[url]" value="%s" class="regular-text" />', $default_url );
$html .= '</td>';
$html .= '</tr>';
echo $html;
}
/**
* Save category custom fields.
*
* @param int $term_id Term ID.
*
* @return update term fields.
*/
public function save_fileds( $term_id ) {
if ( isset( $_POST['category_meta'] ) ) {
$category_meta = get_option( 'category_' . $term_id );
$keys = array_keys( $_POST['category_meta'] );
foreach ( $keys as $key ) {
if ( isset( $_POST['category_meta'][$key] ) ) {
$category_meta[$key] = $_POST['category_meta'][$key];
}
}
// Save the option array.
update_option( 'category_' . $term_id , $category_meta );
}
}
} // close Category_Image_Field class.
$cs_category_image_field = new Category_Image_Field;
/**
* Get category image URL.
*
* @return string Image URL.
*/
function get_cat_image_url() {
// Get the current category ID.
$term_id = get_query_var( 'cat' );
// Get options.
$meta = get_option( 'category_' . $term_id );
if ( isset( $meta['image'] ) ) {
return esc_url( $meta['image'] );
}
}
/**
* Get category meta url.
*
* @return string Meta URL.
*/
function get_cat_url() {
// Get the current category ID.
$term_id = get_query_var( 'cat' );
// Get options.
$meta = get_option( 'category_' . $term_id );
if ( isset( $meta['url'] ) ) {
return esc_url( $meta['url'] );
}
}
/**
* Get category image HTML.
*
* @return string Image HTML.
*/
function get_cat_image() {
// Get the current category ID.
$term_id = get_query_var( 'cat' );
// Get options.
$meta = get_option( 'category_' . $term_id );
if ( isset( $meta['image'] ) ) {
return sprintf( '<a href="%3$s" title="%2$s"><img src="%1$s" alt="%2$s" /></a>', esc_url( $meta['image'] ), single_cat_title( '', false ), esc_url( $meta['url'] ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment