Skip to content

Instantly share code, notes, and snippets.

@ldgarc
Last active December 28, 2017 12:30
Show Gist options
  • Save ldgarc/4dc62220e9136a5af72fe8a3299bb74e to your computer and use it in GitHub Desktop.
Save ldgarc/4dc62220e9136a5af72fe8a3299bb74e to your computer and use it in GitHub Desktop.
Wordpress: Custom Taxonomy Image
<?php // This must be on functions.php
/* Add Image Upload to City Taxonomy */
// Add Upload fields to "Add New Taxonomy" form
function add_city_image_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="city_image"><?php _e( 'City Image:', 'pprealty' ); ?></label>
<input type="text" name="city_image[image]" id="city_image[image]" class="city-image" value="" placeholder="Image Link">
<input class="upload_image_button button" name="_add_city_image" id="_add_city_image" type="button" value="Change Image" />
<script>
jQuery(document).ready(function() {
jQuery('#_add_city_image').click(function() {
wp.media.editor.send.attachment = function(props, attachment) {
jQuery('.city-image').val(attachment.url);
}
wp.media.editor.open(this);
return false;
});
});
</script>
</div>
<?php
}
add_action( 'property-city_add_form_fields', 'add_city_image_field', 10, 2 );
// Add Upload fields to "Edit Taxonomy" form
function pprealty_city_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$cityimage = get_term_meta( $t_id, '_city_image', true); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="_city_image"><?php _e( 'City Image', 'pprealty' ); ?></label></th>
<td>
<?php if (!empty($cityimage)) { ?>
<img id="imagen" src="<?php echo $cityimage["image"]; ?>" style="width: 100%; height: auto;" />
<?php } ?>
<input type="text" name="city_image[image]" id="city_image[image]" class="city-image" value="<?php echo $cityimage["image"]; ?>">
<input class="upload_image_button button" name="_city_image" id="_city_image" type="button" value="Change Image" />
<script>
jQuery(document).ready(function() {
jQuery('#_city_image').click(function() {
wp.media.editor.send.attachment = function(props, attachment) {
jQuery('#imagen').attr("src",attachment.url)
jQuery('.city-image').val(attachment.url)
}
wp.media.editor.open(this);
return false;
});
});
</script>
</td>
</tr>
<?php
}
add_action( 'property-city_edit_form_fields', 'pprealty_city_edit_meta_field', 10, 2 );
// Save Taxonomy Image fields callback function.
function save_city_custom_meta( $term_id ) {
if ( isset( $_POST['city_image'] ) ) {
if ( metadata_exists( 'term', $term_id, '_city_image' ) )
update_term_meta( $term_id, '_city_image', $_POST['city_image']);
else
add_term_meta( $term_id, '_city_image', $_POST['city_image'], true );
}
}
add_action( 'edited_property-city', 'save_city_custom_meta', 10, 2 );
add_action( 'create_property-city', 'save_city_custom_meta', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment