Skip to content
CODEX
  • Home
  • About Us
  • Gallery
  • Contact Us
  • Post & Page
    • Image Field
    • Custom Post Type Filter Admin By Custom Taxonomy
    • How To Export Posts To CSV In WordPress
    • How To Export Custom Posts and Metabox To CSV
  • Post Category
    • Image Field
  • Solution Tips
    • Post type by Year and Month
    • Add a new tab to the category tabs under edit post area
    • How To Export Posts To CSV In WordPress
    • How To Export Custom Posts and Metabox To CSV
  1. Home
  2. Post & Page
  3. Image Field

Image Field

Posted on July 7, 2018 | by admin

Example:

Ref: https://catapultthemes.com/adding-an-image-upload-field-to-categories/

<?php
/*------------------------------------------------------------------------------
Category Banner Uploaded.
-------------------------------------------------------------------------------*/
global $woocommerce;
global $wp_query;
add_action ( 'category_edit_form_fields', 'extra_category_fields');
add_action ( 'category_add_form_fields', 'extra_category_fields');
add_action( 'admin_enqueue_scripts', 'wp_enqueue_media' );
function extra_category_fields( $cat ) { //check for existing featured ID
$t_id = $cat->term_id;
$cat_meta = get_option( "category_$t_id");
?>
<p><img class="custom_media_image" src="<?php if(!empty($cat_meta['img'])){echo $cat_meta['img'];} else { echo 'http://via.placeholder.com/1920x550';} ?>" style="padding:3px;max-width:370px; display:inline-block" /><input type="hidden" class="widefat custom_media_url" name="Cat_meta[img]" id="Cat_meta[img]" value="<?php echo $cat_meta['img'] ? $cat_meta['img'] : ''; ?>"><a style="display:block; width:200px; margin-bottom:20px; text-align:center;" href="#" class="button custom_media_upload"><?php _e('Upload Category Banner', THEMENAME); ?></a></p>
<script>
// Image Upload
(function($){
$('.custom_media_upload').click(function() {
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(this);
wp.media.editor.send.attachment = function(props, attachment) {
$(button).prev().prev().attr('src', attachment.url);
$(button).prev().val(attachment.url);
wp.media.editor.send.attachment = send_attachment_bkp;
}
wp.media.editor.open(button);
return false;
});
})(jQuery);
</script>
<?php
}
add_action ( 'edited_category', 'save_extra_category_fileds');
function save_extra_category_fileds( $term_id ) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['Cat_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['Cat_meta'][$key])){
$cat_meta[$key] = $_POST['Cat_meta'][$key];
}
}
//save the option array
update_option( "category_$t_id", $cat_meta );
}
}

Download Other Feature Categories All Field

Related articles

  • How to bundle themes within plugin?
  • Unregister Widget
  • Add a new tab to the category tabs under edit post area
Posted in Post & Page, Post Category

© 2025 TemplateEYE. All rights reserved.
Power by DeshiSoft