Created
September 2, 2017 07:55
-
-
Save paaljoachim/989279bc07daeb26398f7e599b6c8ab7 to your computer and use it in GitHub Desktop.
Add image to category screen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Made by Isaac: https://gist.github.com/itzikbenh/16326bc3947f272cd6bb44de03c4774b?fref=gc&dti=168889943173228 | |
// and improved by Ian. https://www.facebook.com/groups/advancedwp/permalink/1607185879343620/ | |
function add_category_meta_fields( $taxonomy ) { | |
?> | |
<div class="form-field create-cat term-featured-image-wrap"> | |
<label for="tax-featured-image">Featured Image</label> | |
<input id="tax-featured-image" type="text" name="featured_image"> | |
<br> | |
<button id="add-cat-featured-img" class="button" name="button">Add Featured Image</button> | |
</div> | |
<?php | |
} | |
add_action( 'category_add_form_fields', 'add_category_meta_fields', 10 , 2 ); | |
//This will be used both on creating and updating the category. | |
//It will work since update_term_meta adds if a term doesn't exist. | |
function update_category_meta( $term_id, $tt_id ) { | |
if ( isset( $_POST['featured_image'] ) ) { | |
$featured_image = sanitize_text_field( trim( $_POST['featured_image'] ) ); | |
update_term_meta( $term_id, 'featured_image', $featured_image ); | |
} | |
} | |
add_action( 'edited_category', 'update_category_meta', 10, 2 ); | |
add_action( 'created_category', 'update_category_meta', 10, 2 ); | |
function edit_category_meta_fields( $term, $taxonomy ) { | |
$featured_image = get_term_meta( $term->term_id, 'featured_image', true ); | |
?> | |
<tr class="form-field term-featured-image-group"> | |
<th scope="row"> | |
<label for="featured-image">Featured Image</label> | |
</th> | |
<td> | |
<img src="<?php echo $featured_image; ?>" alt="" class="tax-featured-image <?php echo $featured_image ? '' : 'hidden'; ?> "> | |
<input id="tax-featured-image" type="hidden" name="featured_image" value="<?php echo $featured_image; ?>"> | |
<?php if ( $featured_image ): ?> | |
<button id="update-cat-featured-img" class="button" name="button">Update Featured Image</button> | |
<button id="remove-cat-featured-img" class="button" name="button">Remove Featured Image</button> | |
<?php else: ?> | |
<button id="update-cat-featured-img" class="button" name="button">Add Featured Image</button> | |
<?php endif; ?> | |
</td> | |
</tr> | |
<?php | |
} | |
add_action( 'category_edit_form_fields', 'edit_category_meta_fields', 10, 2 ); | |
function update_script() { ?> | |
<script> | |
jQuery('#update-cat-featured-img, #add-cat-featured-img').click(function(e) { | |
e.preventDefault(); | |
var custom_uploader = wp.media({ | |
title: 'Featured Image', | |
button: { | |
text: 'Insert image' | |
}, | |
multiple: false // Set this to "add" to allow multiple files to be selected | |
}) | |
.on('select', function() { | |
var attachment = custom_uploader.state().get('selection').first().toJSON(); | |
jQuery('.tax-featured-image').attr('src', attachment.sizes.thumbnail.url).show(); | |
jQuery('#tax-featured-image').val(attachment.url); | |
}) | |
.open(); | |
}); | |
jQuery('#remove-cat-featured-img').click(function(e) { | |
e.preventDefault(); | |
jQuery('.tax-featured-image').attr('src', ''); | |
jQuery('#tax-featured-image').val('noimage'); | |
}); | |
</script> | |
<?php } | |
add_action( 'admin_footer', 'update_script' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment