How to add custom field in wordpress post taxonomy or in other custom taxonomy just like set feature image in taxonomy
Just copy and paste below code into your theme's functions.php file or into your custom plugin file and change taxonomy slug according to your requirement.
<?php
// Here i used custom taxonomy people_cat , You can also use post's default taxonomy like category , tag
add_action( 'people_cat_add_form_fields', '_rj_add_category_fields' );
add_action( 'people_cat_edit_form_fields','_rj_edit_category_fields');
function _rj_add_category_fields($taxonomy)
{
global $wpdb;
?>
<div class="form-field">
<label><?php _e( 'Thumbnail', 'restlessdance' ); ?></label>
<div id="<?php echo $taxonomy; ?>_thumbnail" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( rj_placeholder_img_src() ); ?>" width="60px" height="60px" /></div>
<div style="line-height: 60px;">
<input type="hidden" id="<?php echo $taxonomy; ?>_thumbnail_id" name="<?php echo $taxonomy; ?>_thumbnail_id" />
<button type="button" class="upload_image_button button"><?php _e( 'Upload/Add image', 'restlessdance' ); ?></button>
<button type="button" class="remove_image_button button"><?php _e( 'Remove image', 'restlessdance' ); ?></button>
</div>
<script type="text/javascript">
if ( ! jQuery( '#<?php echo $taxonomy; ?>_thumbnail_id' ).val() ) {
jQuery( '.remove_image_button' ).hide();
}
var file_frame;
jQuery( document ).on( 'click', '.upload_image_button', function( event ) {
event.preventDefault();
if ( file_frame ) {
file_frame.open();
return;
}
file_frame = wp.media.frames.downloadable_file = wp.media({
title: '<?php _e( "Choose an image", "restlessdance" ); ?>',
button: {
text: '<?php _e( "Use image", "restlessdance" ); ?>'
},
multiple: false
});
file_frame.on( 'select', function() {
var attachment = file_frame.state().get( 'selection' ).first().toJSON();
jQuery( '#<?php echo $taxonomy; ?>_thumbnail_id' ).val( attachment.id );
jQuery( '#<?php echo $taxonomy; ?>_thumbnail img' ).attr( 'src', attachment.sizes.thumbnail.url );
jQuery( '.remove_image_button' ).show();
});
file_frame.open();
});
jQuery( document ).on( 'click', '.remove_image_button', function() {
jQuery( '#<?php echo $taxonomy; ?>_thumbnail img' ).attr( 'src', '<?php echo esc_js( rj_placeholder_img_src() ); ?>' );
jQuery( '#<?php echo $taxonomy; ?>_thumbnail_id' ).val( '' );
jQuery( '.remove_image_button' ).hide();
return false;
});
</script>
<div class="clear"></div>
</div>
<?php
}
function _rj_edit_category_fields( $term )
{
global $wpdb;
$term_id = $term->term_id;
$thumbnail_id = get_option( $term->taxonomy.'_'.$term_id.'_thumbnail_id');
if ( $thumbnail_id ) {
$image = wp_get_attachment_thumb_url( $thumbnail_id );
} else {
$image = rj_placeholder_img_src();
}
?>
<tr class="form-field">
<th scope="row" valign="top"><label><?php _e( 'Thumbnail', 'restlessdance' ); ?></label></th>
<td>
<div id="<?php echo $term->taxonomy; ?>_thumbnail" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( $image ); ?>" width="60px" height="60px" /></div>
<div style="line-height: 60px;">
<input type="hidden" id="<?php echo $term->taxonomy; ?>_thumbnail_id" name="<?php echo $term->taxonomy; ?>_thumbnail_id" value="<?php echo $thumbnail_id; ?>" />
<button type="button" class="upload_image_button button"><?php _e( 'Upload/Add image', 'restlessdance' ); ?></button>
<button type="button" class="remove_image_button button"><?php _e( 'Remove image', 'restlessdance' ); ?></button>
</div>
<script type="text/javascript">
if ( '0' === jQuery( '#<?php echo $term->taxonomy; ?>_thumbnail_id' ).val() ) {
jQuery( '.remove_image_button' ).hide();
}
var file_frame;
jQuery( document ).on( 'click', '.upload_image_button', function( event ) {
event.preventDefault();
if ( file_frame ) {
file_frame.open();
return;
}
file_frame = wp.media.frames.downloadable_file = wp.media({
title: '<?php _e( "Choose an image", "restlessdance" ); ?>',
button: {
text: '<?php _e( "Use image", "restlessdance" ); ?>'
},
multiple: false
});
file_frame.on( 'select', function() {
var attachment = file_frame.state().get( 'selection' ).first().toJSON();
jQuery( '#<?php echo $term->taxonomy; ?>_thumbnail_id' ).val( attachment.id );
jQuery( '#<?php echo $term->taxonomy; ?>_thumbnail img' ).attr( 'src', attachment.sizes.thumbnail.url );
jQuery( '.remove_image_button' ).show();
});
file_frame.open();
});
jQuery( document ).on( 'click', '.remove_image_button', function() {
jQuery( '#<?php echo $term->taxonomy; ?>_thumbnail img' ).attr( 'src', '<?php echo esc_js( rj_placeholder_img_src() ); ?>' );
jQuery( '#<?php echo $term->taxonomy; ?>_thumbnail_id' ).val( '' );
jQuery( '.remove_image_button' ).hide();
return false;
});
</script>
<div class="clear"></div>
</td>
</tr>
<?php
}
add_action( 'created_term','_rj_save_cat_fields' ,10, 3 );
add_action( 'edit_term', '_rj_save_cat_fields' , 10, 3 );
function _rj_save_cat_fields( $term_id, $tt_id = '', $taxonomy = '' ) {
global $wpdb;
if ( isset( $_POST[$taxonomy.'_thumbnail_id'] ) && ('people_cat' === $taxonomy ) ) {
update_option($taxonomy.'_'.$term_id.'_thumbnail_id',absint( $_POST[$taxonomy.'_thumbnail_id'] ));
}
}
add_filter( 'manage_edit-people_cat_columns', 'rj_manage_cat_columns' );
function rj_manage_cat_columns( $columns ) {
$new_columns = array();
$new_columns['cb'] = $columns['cb'];
$new_columns['thumb'] = __( 'Image', 'restlessdance' );
unset( $columns['cb'] );
return array_merge( $new_columns, $columns );
}
add_filter( 'manage_people_cat_custom_column', '_rj_manage_people_cat_image_column', 10, 3 );
function _rj_manage_shows_cat_image_column( $columns, $column, $id ) {
if ( 'thumb' == $column )
{
$thumbnail_id = get_option('shows_cat_'.$id.'_thumbnail_id');
if ( $thumbnail_id ) {
$image = wp_get_attachment_thumb_url( absint($thumbnail_id) );
} else {
$image = rj_placeholder_img_src();
}
$image = str_replace( ' ', '%20', $image );
$columns .= '<img src="' . esc_url( $image ) . '" alt="' . __( 'Thumbnail', 'restlessdance' ) . '" class="wp-post-image" height="48" width="48" />';
}
return $columns;
}
function _rj_manage_people_cat_image_column( $columns, $column, $id ) {
if ( 'thumb' == $column )
{
$thumbnail_id = get_option('people_cat_'.$id.'_thumbnail_id');
if ( $thumbnail_id ) {
$image = wp_get_attachment_thumb_url( absint($thumbnail_id) );
} else {
$image = rj_placeholder_img_src();
}
$image = str_replace( ' ', '%20', $image );
$columns .= '<img src="' . esc_url( $image ) . '" alt="' . __( 'Thumbnail', 'restlessdance' ) . '" class="wp-post-image" height="48" width="48" />';
}
return $columns;
}
function _rj_manage_workshop_cat_image_column( $columns, $column, $id ) {
if ( 'thumb' == $column )
{
$thumbnail_id = get_option('workshop_cat_'.$id.'_thumbnail_id');
if ( $thumbnail_id ) {
$image = wp_get_attachment_thumb_url( absint($thumbnail_id) );
} else {
$image = rj_placeholder_img_src();
}
$image = str_replace( ' ', '%20', $image );
$columns .= '<img src="' . esc_url( $image ) . '" alt="' . __( 'Thumbnail', 'restlessdance' ) . '" class="wp-post-image" height="48" width="48" />';
}
return $columns;
}
function _rj_manage_resource_cat_image_column( $columns, $column, $id ) {
if ( 'thumb' == $column )
{
$thumbnail_id = get_option('resource_cat_'.$id.'_thumbnail_id');
if ( $thumbnail_id ) {
$image = wp_get_attachment_thumb_url( absint($thumbnail_id) );
} else {
$image = rj_placeholder_img_src();
}
$image = str_replace( ' ', '%20', $image );
$columns .= '<img src="' . esc_url( $image ) . '" alt="' . __( 'Thumbnail', 'restlessdance' ) . '" class="wp-post-image" height="48" width="48" />';
}
return $columns;
}
function rj_load_wp_media_files() {
wp_enqueue_media();
}
add_action( 'admin_enqueue_scripts', 'rj_load_wp_media_files' );
<?php
// Here i used custom taxonomy people_cat , You can also use post's default taxonomy like category , tag
add_action( 'people_cat_add_form_fields', '_rj_add_category_fields' );
add_action( 'people_cat_edit_form_fields','_rj_edit_category_fields');
function _rj_add_category_fields($taxonomy)
{
global $wpdb;
?>
<div class="form-field">
<label><?php _e( 'Thumbnail', 'restlessdance' ); ?></label>
<div id="<?php echo $taxonomy; ?>_thumbnail" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( rj_placeholder_img_src() ); ?>" width="60px" height="60px" /></div>
<div style="line-height: 60px;">
<input type="hidden" id="<?php echo $taxonomy; ?>_thumbnail_id" name="<?php echo $taxonomy; ?>_thumbnail_id" />
<button type="button" class="upload_image_button button"><?php _e( 'Upload/Add image', 'restlessdance' ); ?></button>
<button type="button" class="remove_image_button button"><?php _e( 'Remove image', 'restlessdance' ); ?></button>
</div>
<script type="text/javascript">
if ( ! jQuery( '#<?php echo $taxonomy; ?>_thumbnail_id' ).val() ) {
jQuery( '.remove_image_button' ).hide();
}
var file_frame;
jQuery( document ).on( 'click', '.upload_image_button', function( event ) {
event.preventDefault();
if ( file_frame ) {
file_frame.open();
return;
}
file_frame = wp.media.frames.downloadable_file = wp.media({
title: '<?php _e( "Choose an image", "restlessdance" ); ?>',
button: {
text: '<?php _e( "Use image", "restlessdance" ); ?>'
},
multiple: false
});
file_frame.on( 'select', function() {
var attachment = file_frame.state().get( 'selection' ).first().toJSON();
jQuery( '#<?php echo $taxonomy; ?>_thumbnail_id' ).val( attachment.id );
jQuery( '#<?php echo $taxonomy; ?>_thumbnail img' ).attr( 'src', attachment.sizes.thumbnail.url );
jQuery( '.remove_image_button' ).show();
});
file_frame.open();
});
jQuery( document ).on( 'click', '.remove_image_button', function() {
jQuery( '#<?php echo $taxonomy; ?>_thumbnail img' ).attr( 'src', '<?php echo esc_js( rj_placeholder_img_src() ); ?>' );
jQuery( '#<?php echo $taxonomy; ?>_thumbnail_id' ).val( '' );
jQuery( '.remove_image_button' ).hide();
return false;
});
</script>
<div class="clear"></div>
</div>
<?php
}
function _rj_edit_category_fields( $term )
{
global $wpdb;
$term_id = $term->term_id;
$thumbnail_id = get_option( $term->taxonomy.'_'.$term_id.'_thumbnail_id');
if ( $thumbnail_id ) {
$image = wp_get_attachment_thumb_url( $thumbnail_id );
} else {
$image = rj_placeholder_img_src();
}
?>
<tr class="form-field">
<th scope="row" valign="top"><label><?php _e( 'Thumbnail', 'restlessdance' ); ?></label></th>
<td>
<div id="<?php echo $term->taxonomy; ?>_thumbnail" style="float: left; margin-right: 10px;"><img src="<?php echo esc_url( $image ); ?>" width="60px" height="60px" /></div>
<div style="line-height: 60px;">
<input type="hidden" id="<?php echo $term->taxonomy; ?>_thumbnail_id" name="<?php echo $term->taxonomy; ?>_thumbnail_id" value="<?php echo $thumbnail_id; ?>" />
<button type="button" class="upload_image_button button"><?php _e( 'Upload/Add image', 'restlessdance' ); ?></button>
<button type="button" class="remove_image_button button"><?php _e( 'Remove image', 'restlessdance' ); ?></button>
</div>
<script type="text/javascript">
if ( '0' === jQuery( '#<?php echo $term->taxonomy; ?>_thumbnail_id' ).val() ) {
jQuery( '.remove_image_button' ).hide();
}
var file_frame;
jQuery( document ).on( 'click', '.upload_image_button', function( event ) {
event.preventDefault();
if ( file_frame ) {
file_frame.open();
return;
}
file_frame = wp.media.frames.downloadable_file = wp.media({
title: '<?php _e( "Choose an image", "restlessdance" ); ?>',
button: {
text: '<?php _e( "Use image", "restlessdance" ); ?>'
},
multiple: false
});
file_frame.on( 'select', function() {
var attachment = file_frame.state().get( 'selection' ).first().toJSON();
jQuery( '#<?php echo $term->taxonomy; ?>_thumbnail_id' ).val( attachment.id );
jQuery( '#<?php echo $term->taxonomy; ?>_thumbnail img' ).attr( 'src', attachment.sizes.thumbnail.url );
jQuery( '.remove_image_button' ).show();
});
file_frame.open();
});
jQuery( document ).on( 'click', '.remove_image_button', function() {
jQuery( '#<?php echo $term->taxonomy; ?>_thumbnail img' ).attr( 'src', '<?php echo esc_js( rj_placeholder_img_src() ); ?>' );
jQuery( '#<?php echo $term->taxonomy; ?>_thumbnail_id' ).val( '' );
jQuery( '.remove_image_button' ).hide();
return false;
});
</script>
<div class="clear"></div>
</td>
</tr>
<?php
}
add_action( 'created_term','_rj_save_cat_fields' ,10, 3 );
add_action( 'edit_term', '_rj_save_cat_fields' , 10, 3 );
function _rj_save_cat_fields( $term_id, $tt_id = '', $taxonomy = '' ) {
global $wpdb;
if ( isset( $_POST[$taxonomy.'_thumbnail_id'] ) && ('people_cat' === $taxonomy ) ) {
update_option($taxonomy.'_'.$term_id.'_thumbnail_id',absint( $_POST[$taxonomy.'_thumbnail_id'] ));
}
}
add_filter( 'manage_edit-people_cat_columns', 'rj_manage_cat_columns' );
function rj_manage_cat_columns( $columns ) {
$new_columns = array();
$new_columns['cb'] = $columns['cb'];
$new_columns['thumb'] = __( 'Image', 'restlessdance' );
unset( $columns['cb'] );
return array_merge( $new_columns, $columns );
}
add_filter( 'manage_people_cat_custom_column', '_rj_manage_people_cat_image_column', 10, 3 );
function _rj_manage_shows_cat_image_column( $columns, $column, $id ) {
if ( 'thumb' == $column )
{
$thumbnail_id = get_option('shows_cat_'.$id.'_thumbnail_id');
if ( $thumbnail_id ) {
$image = wp_get_attachment_thumb_url( absint($thumbnail_id) );
} else {
$image = rj_placeholder_img_src();
}
$image = str_replace( ' ', '%20', $image );
$columns .= '<img src="' . esc_url( $image ) . '" alt="' . __( 'Thumbnail', 'restlessdance' ) . '" class="wp-post-image" height="48" width="48" />';
}
return $columns;
}
function _rj_manage_people_cat_image_column( $columns, $column, $id ) {
if ( 'thumb' == $column )
{
$thumbnail_id = get_option('people_cat_'.$id.'_thumbnail_id');
if ( $thumbnail_id ) {
$image = wp_get_attachment_thumb_url( absint($thumbnail_id) );
} else {
$image = rj_placeholder_img_src();
}
$image = str_replace( ' ', '%20', $image );
$columns .= '<img src="' . esc_url( $image ) . '" alt="' . __( 'Thumbnail', 'restlessdance' ) . '" class="wp-post-image" height="48" width="48" />';
}
return $columns;
}
function _rj_manage_workshop_cat_image_column( $columns, $column, $id ) {
if ( 'thumb' == $column )
{
$thumbnail_id = get_option('workshop_cat_'.$id.'_thumbnail_id');
if ( $thumbnail_id ) {
$image = wp_get_attachment_thumb_url( absint($thumbnail_id) );
} else {
$image = rj_placeholder_img_src();
}
$image = str_replace( ' ', '%20', $image );
$columns .= '<img src="' . esc_url( $image ) . '" alt="' . __( 'Thumbnail', 'restlessdance' ) . '" class="wp-post-image" height="48" width="48" />';
}
return $columns;
}
function _rj_manage_resource_cat_image_column( $columns, $column, $id ) {
if ( 'thumb' == $column )
{
$thumbnail_id = get_option('resource_cat_'.$id.'_thumbnail_id');
if ( $thumbnail_id ) {
$image = wp_get_attachment_thumb_url( absint($thumbnail_id) );
} else {
$image = rj_placeholder_img_src();
}
$image = str_replace( ' ', '%20', $image );
$columns .= '<img src="' . esc_url( $image ) . '" alt="' . __( 'Thumbnail', 'restlessdance' ) . '" class="wp-post-image" height="48" width="48" />';
}
return $columns;
}
function rj_load_wp_media_files() {
wp_enqueue_media();
}
add_action( 'admin_enqueue_scripts', 'rj_load_wp_media_files' );
Comments
Post a Comment