Created
August 26, 2017 23:14
-
-
Save itzikbenh/16326bc3947f272cd6bb44de03c4774b to your computer and use it in GitHub Desktop.
Category Image
This file contains 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
$('#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(); | |
$('.tax-featured-image').attr('src', attachment.url).show(); | |
$('#tax-featured-image').val(attachment.url); | |
}) | |
.open(); | |
}); |
This file contains 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
<?php | |
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> | |
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment