Skip to content

Instantly share code, notes, and snippets.

@jaredrethman
Last active November 22, 2017 01:10
Show Gist options
  • Save jaredrethman/8c2f038fe4b82f1d3210 to your computer and use it in GitHub Desktop.
Save jaredrethman/8c2f038fe4b82f1d3210 to your computer and use it in GitHub Desktop.
Small helper class for Wordpress Taxonomy image upload
//Based off - https://gist.github.com/mathetos/1eea92f71934442671a7
class Tax_Images {
public function __construct() {
//Enqueue Media JS/CSS
add_action( 'admin_enqueue_scripts', 'wp_enqueue_media' );
add_action( 'category_add_form_fields' , array( $this, 'ti_add_form_fields' ), 10, 2 );
add_action( 'category_edit_form_fields' , array( $this, 'ti_edit_form_fields' ), 10, 2 );
add_action( 'edited_category' , array( $this, 'ti_save_category_fields' ), 10, 2 );
add_action( 'create_category' , array( $this, 'ti_save_category_fields' ), 10, 2 );
}
//Add the form fields
public static function ti_add_form_fields( ) {
?>
<div class="form-field">
<label for="tax_img"><?php _e( 'Series Image:', 'journey' ); ?></label>
<input type="text" name="tax_img[image]" id="tax_img[image]" class="tax-img" value="">
<input class="upload_image_button button" name="_add_tax_image" id="_add_tax_image" type="button" value="Select/Upload Image" />
<script>
jQuery(document).ready(function() {
jQuery('#_add_tax_image').click(function() {
wp.media.editor.send.attachment = function(props, attachment) {
jQuery('.tax-img').val(attachment.url);
}
wp.media.editor.open(this);
return false;
});
});
</script>
</div>
<?php
}
public static function ti_edit_form_fields( $term ) {
$t_id = $term->term_id;
$term_meta = get_option( "tax_img_" . $t_id ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="_tax_img"><?php _e( 'Category Image', 'journey' ); ?></label></th>
<td>
<?php
$tax_img = esc_attr( $term_meta['image'] ) ? esc_attr( $term_meta['image'] ) : '';
?>
<input type="text" name="tax_img[image]" id="tax_img[image]" class="tax_img-image" value="<?php echo $tax_img; ?>">
<input class="upload_image_button button" name="_tax_img" id="_tax_img" type="button" value="Select/Upload Image" />
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"></th>
<td style="height: 150px;">
<style>
div.img-wrap {
background: url('http://placehold.it/960x300') no-repeat center;
background-size:contain;
max-width: 450px;
max-height: 150px;
width: 100%;
height: 100%;
overflow:hidden;
}
div.img-wrap img {
max-width: 450px;
}
</style>
<div class="img-wrap">
<img src="<?php echo $tax_img; ?>" id="tax-img">
</div>
<script>
jQuery(document).ready(function() {
jQuery('#_tax_img').click(function() {
wp.media.editor.send.attachment = function(props, attachment) {
jQuery('#tax-img').attr("src",attachment.url)
jQuery('.tax_img-image').val(attachment.url)
}
wp.media.editor.open(this);
return false;
});
});
</script>
</td>
</tr>
<?php
}
public static function ti_save_category_fields( $term_id ) {
if ( isset( $_POST['tax_img'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "tax_img_$t_id" );
$cat_keys = array_keys( $_POST['tax_img'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['tax_img'][$key] ) ) {
$term_meta[$key] = $_POST['tax_img'][$key];
}
}
// Save the option array.
update_option( "tax_img_$t_id", $term_meta );
}
}
}
new Tax_Images;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment