true ], 'names' );
foreach( $taxes as $taxname ){
add_action( "{$taxname}_add_form_fields", [ $this, 'add_term_image' ], 10, 2 );
add_action( "{$taxname}_edit_form_fields", [ $this, 'update_term_image' ], 10, 2 );
add_action( "created_{$taxname}", [ $this, 'save_term_image' ], 10, 2 );
add_action( "edited_{$taxname}", [ $this, 'updated_term_image' ], 10, 2 );
add_filter( "manage_edit-{$taxname}_columns", [ $this, 'add_image_column' ] );
add_filter( "manage_{$taxname}_custom_column", [ $this, 'fill_image_column' ], 10, 3 );
}
}
## поля при создании термина
public function add_term_image( $taxonomy ){
wp_enqueue_media(); // подключим стили медиа, если их нет
add_action('admin_print_footer_scripts', [ $this, 'add_script' ], 99 );
$this->css();
?>
term_id, self::$meta_key, true );
$image_url = $image_id ? wp_get_attachment_image_url( $image_id, 'thumbnail' ) : self::$add_img_url;
$this->css();
?>
base, array('edit-tags','term')) || ! in_array($cs->taxonomy, (array) $this->for_taxes) )
// return;
$title = __('Featured Image', 'default');
$button_txt = __('Set featured image', 'default');
?>
.column-image{ width:50px; text-align:center; }
';
});
// column without name
return array_slice( $columns, 0, 1 ) + [ 'image' =>'' ] + $columns;
}
public function fill_image_column( $string, $column_name, $term_id ){
if( 'image' === $column_name && $image_id = get_term_meta( $term_id, self::$meta_key, 1 ) ){
$string = '';
}
return $string;
}
## Save the form field
public function save_term_image( $term_id, $tt_id ){
if( isset($_POST['term_imgid']) && $attach_id = (int) $_POST['term_imgid'] ){
update_term_meta( $term_id, self::$meta_key, $attach_id );
update_post_meta( $attach_id, self::$attach_term_meta_key, $term_id );
}
}
## Update the form field value
public function updated_term_image( $term_id, $tt_id ){
if( ! isset($_POST['term_imgid']) )
return;
$cur_term_attach_id = (int) get_term_meta( $term_id, self::$meta_key, 1 );
if( $attach_id = (int) $_POST['term_imgid'] ){
update_term_meta( $term_id, self::$meta_key, $attach_id );
update_post_meta( $attach_id, self::$attach_term_meta_key, $term_id );
if( $cur_term_attach_id != $attach_id )
wp_delete_attachment( $cur_term_attach_id );
}
else {
if( $cur_term_attach_id )
wp_delete_attachment( $cur_term_attach_id );
delete_term_meta( $term_id, self::$meta_key );
}
}
}
}
/**
* 3.0 - 2019-04-24 - Баг: колонка заполнялась без проверки имени колонки.
* 2.9 Добавил метаполе для вложений (img_term), где хранится ID термина к которому прикреплено вложение.
* Добавил физическое удаление картинки (файла вложения) при удалении его у термина.
* 2.8 Исправил ошибку удаления картинки.
*/