Skip to content

Instantly share code, notes, and snippets.

@claudiosanches
Last active November 18, 2017 19:23
Show Gist options
  • Select an option

  • Save claudiosanches/4498872 to your computer and use it in GitHub Desktop.

Select an option

Save claudiosanches/4498872 to your computer and use it in GitHub Desktop.

Revisions

  1. claudiosanches revised this gist Jan 10, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion category-image-field.php
    Original file line number Diff line number Diff line change
    @@ -72,7 +72,7 @@ public function register_fields( $term ) {
    $html .= '</td>';
    $html .= '</tr>';

    $html = '<tr>';
    $html .= '<tr>';
    $html .= '<th valign="top" scope="row">';
    $html .= sprintf( '<label for="category-meta-url">%s</label>', __( 'URL', 'cif' ) );
    $html .= '</th>';
  2. claudiosanches revised this gist Jan 10, 2013. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions category-image-field.php
    Original file line number Diff line number Diff line change
    @@ -126,6 +126,22 @@ function get_cat_image_url() {
    }
    }

    /**
    * 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.
    *
  3. claudiosanches revised this gist Jan 10, 2013. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion category-image-field.php
    Original file line number Diff line number Diff line change
    @@ -42,6 +42,7 @@ function scripts() {
    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">';
    @@ -71,6 +72,15 @@ public function register_fields( $term ) {
    $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;
    }

    @@ -128,6 +138,6 @@ function get_cat_image() {
    $meta = get_option( 'category_' . $term_id );

    if ( isset( $meta['image'] ) ) {
    return sprintf( '<img src="%1$s" alt="%2$s" title="%2$s" />', esc_url( $meta['image'] ), single_cat_title( '', false ) );
    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'] ) );
    }
    }
  4. claudiosanches created this gist Jan 10, 2013.
    133 changes: 133 additions & 0 deletions category-image-field.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,133 @@
    <?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'] : '';

    $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>';

    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 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( '<img src="%1$s" alt="%2$s" title="%2$s" />', esc_url( $meta['image'] ), single_cat_title( '', false ) );
    }
    }