This question already has answers here: Add custom field to Category (4 answers) Closed 3 years ago.admin管理员组文章数量:1302360
I'm creating a website, and I want to associate images every time I create a new category? Does anyone know how I give this option to the user through this menu?
This question already has answers here: Add custom field to Category (4 answers) Closed 3 years ago.I'm creating a website, and I want to associate images every time I create a new category? Does anyone know how I give this option to the user through this menu?
Share Improve this question edited Mar 17, 2021 at 21:12 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Mar 17, 2021 at 20:08 Leonardo Alves Leonardo Alves 32 bronze badges 01 Answer
Reset to default 2Simply add this code to functions.php file
add_action ( 'category_add_form_fields', '___add_form_field_term_meta_text' );
function ___add_form_field_term_meta_text() {
?>
<div class="form-field custom_image_upload">
<label for="tag-description">File Upload</label>
<img src="" style="width:100px;height:100px;border:0;display:none;" />
<a title="Set listing image" href="javascript:;" id="upload_listing_image_button" id="set-listing-image" data-uploader_title="Choose an image" data-uploader_button_text="Set listing image">Set listing image</a>
<input type="hidden" id="upload_listing_image" name="_listing_cover_image" value="" />
</div>
<?php
}
add_action ( 'category_edit_form_fields', '___edit_form_field_term_meta_text' );
function ___edit_form_field_term_meta_text($term) {
$image_id = get_term_meta ( $term->term_id, '_listing_cover_image', true );
if( empty ( $image_id ) ) {
$display_none = 'display:none;';
$upload_link = '<a title="Set listing image" href="javascript:;" id="upload_listing_image_button" id="set-listing-image" data-uploader_title="Choose an image" data-uploader_button_text="Set listing image">Set listing image</a>';
} else {
$display_none = '';
$upload_link = '<a title="Set listing image" href="javascript:;" id="remove_listing_image_button" data-uploader_title="Choose an image" data-uploader_button_text="Set listing image">Remove listing image</a>';
}
?>
<tr class="form-field custom_image_upload">
<th scope="row"><label for="term-meta-text"><?php _e ( 'File Upload', 'text_domain' ); ?></label></th>
<td>
<img src="<?php echo wp_get_attachment_image_src ( $image_id )[ 0 ]; ?>" style="width:100px;height:100px;border:0;<?php echo $display_none; ?>" />
<?php echo $upload_link; ?>
<input type="hidden" id="upload_listing_image" name="_listing_cover_image" value="<?php echo $image_id; ?>" />
</td>
</tr>
<?php
}
add_action ( 'edit_category', '___save_term_meta_text' );
add_action ( 'create_category', '___save_term_meta_text' );
function ___save_term_meta_text($term_id) {
if( isset ( $_POST[ '_listing_cover_image' ] ) ) {
$image_id = (int) $_POST[ '_listing_cover_image' ];
update_term_meta ( $term_id, '_listing_cover_image', $image_id );
}
}
add_filter ( 'manage_edit-category_columns', '___edit_term_columns' );
function ___edit_term_columns($columns) {
$columns[ '__term_meta_text' ] = __ ( 'Image', 'text_domain' );
return $columns;
}
// RENDER COLUMNS (render the meta data on a column)
add_filter ( 'manage_category_custom_column', '___manage_term_custom_column', 10, 3 );
function ___manage_term_custom_column($out, $column, $term_id) {
if( '__term_meta_text' === $column ) {
$image_id = get_term_meta ( $term_id, '_listing_cover_image', true );
if( ! empty ( $image_id ) ) {
$out = '<span class="term-meta-text-block" style="" ><img src="' . wp_get_attachment_image_src ( $image_id )[ 0 ] . '"style="width:50px;height:50px;border:0;" /></div>';
}
}
return $out;
}
add following code in registered admin javascript file which is enqueue via admin_enqueue_scripts
ex. mine is admin-script.js
jQuery(document).ready(function ($) {
// Uploading files
var file_frame;
jQuery.fn.upload_listing_image = function (button) {
var button_id = button.attr('id');
var field_id = button_id.replace('_button', '');
// If the media frame already exists, reopen it.
if (file_frame) {
file_frame.open();
return;
}
// Create the media frame.
file_frame = wp.media.frames.file_frame = wp.media({
title: jQuery(this).data('uploader_title'),
button: {
text: jQuery(this).data('uploader_button_text'),
},
multiple: false
});
// When an image is selected, run a callback.
file_frame.on('select', function () {
var attachment = file_frame.state().get('selection').first().toJSON();
jQuery("#" + field_id).val(attachment.id);
jQuery(".custom_image_upload img").attr('src', attachment.url);
jQuery('.custom_image_upload img').show();
jQuery('#' + button_id).attr('id', 'remove_listing_image_button');
jQuery('#remove_listing_image_button').text('Remove listing image');
});
// Finally, open the modal
file_frame.open();
};
jQuery('.custom_image_upload').on('click', '#upload_listing_image_button', function (event) {
event.preventDefault();
jQuery.fn.upload_listing_image(jQuery(this));
});
jQuery('.custom_image_upload').on('click', '#remove_listing_image_button', function (event) {
event.preventDefault();
jQuery('#upload_listing_image').val('');
jQuery('.custom_image_upload img').attr('src', '');
jQuery('.custom_image_upload img').hide();
jQuery(this).attr('id', 'upload_listing_image_button');
jQuery('#upload_listing_image_button').text('Set listing image');
});
jQuery(document).ajaxSuccess(function (event, xhr, settings) {
if (settings.data.indexOf('add-tag') > -1) {
jQuery(document).find('#remove_listing_image_button').trigger('click');
}
});
});
you should get result like this
本文标签: categoriesHow to create a custom meta box field in category
版权声明:本文标题:categories - How to create a custom meta box field in category? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741668841a2391479.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论