Ngày mới An Nhiên » WordPress » Thủ thuật WordPress » Thêm custom meta fields cho taxonomies trong WordPress

Thêm custom meta fields cho taxonomies trong WordPress

Việc tuỳ chỉnh các trường cho custom post_type, taxonomy là việc thường gặp nếu bạn phát triển plugins, themes theo từng yêu cầu cụ thể.

Đoạn mã sau là ví dụ để bạn có thể thêm 1 trường tuỳ chỉnh cho taxonomy tuỳ chỉnh của bạn. Ở đây, của mình là 1 custom taxonomy tên suppliers do đó mình sẽ add_action theo định dạng bắt buộc là:

{$taxonomy_name}_add_form_fields

Trong đó taxonomy_name mặc định cho các categories mặc địnhcategory

Khi đó nếu bạn custom các trường cho danh mục mặc định của WordPress thì bạn sẽ thêm 2 action như sau:

//Cho phần thêm category;
add_action( '<strong>category</strong>_add_form_fields', 'hoydiha_goodluck_to_you_meta_field_add', 20, 2 );
//Chi phần chỉnh sửa category;
add_action( '<strong>category</strong>_edit_form_fields', 'hoydiha_goodluck_to_you_meta_field_edit', 10, 2 );

Khi đó nếu bạn custom các trường trong hàm offers_supllier_affiliate_meta_field, offers_supllier_affiliate_meta_field_edit (bạn có thể đặt tên hàm tuỳ ý) thì mình sẽ add các action sau:

//Cho phần thêm suppliers;
add_action( 'suppliers_add_form_fields', 'offers_supllier_affiliate_meta_field', 20, 2 );
//Chi phần chỉnh sửa suppliers;
add_action( 'suppliers_edit_form_fields', 'offers_supllier_affiliate_meta_field_edit', 10, 2 );

Toàn bộ mã tuỳ chỉnh của ta đầy đủ như sau:

 

Đăng ký tham gia kiếm tiền online với nền tảng tiếp thị liên kết tại: Dinos | ACCESSTRADE | AT D2C | DiMuaDi
Sau đó tải miễn phí bộ plugins hỗ trợ xây dựng web/ blog mã giảm giá bán tự động PXG Coupons

/*88OffersSuppliers-addmetabox*/
function save_offers_supllier_custom_meta( $term_id ) {
	if ( isset( $_POST['term_meta'] ) ) {
		$t_id = $term_id;
		$term_meta = get_option( "taxonomy_$t_id" );
		$cat_keys = array_keys( $_POST['term_meta'] );
		foreach ( $cat_keys as $key ) {
			if ( isset ( $_POST['term_meta'][$key] ) ) {
				$term_meta[$key] = $_POST['term_meta'][$key];
			}
		}
		// Save the option array.
		update_option( "taxonomy_$t_id", $term_meta );
	}
}  
add_action( 'edited_suppliers', 'save_offers_supllier_custom_meta', 10, 2 );  
add_action( 'create_suppliers', 'save_offers_supllier_custom_meta', 10, 2 );
function offers_supllier_affiliate_meta_field() {
	?>
	<div class="form-field">
		<label for="term_meta[offers_supllier_affiliate_url]"><?php _e( 'Affiliate url', '88offers' ); ?></label>
		<input type="text" name="term_meta[offers_supllier_affiliate_url]" id="term_meta[offers_supllier_affiliate_url]" value="">
		<p class="description"><?php _e( 'Nhập affiliate url','88offers' ); ?></p>
	</div>
<?php
}
//taxonomy:category, suppliers
add_action( 'suppliers_add_form_fields', 'offers_supllier_affiliate_meta_field', 20, 2 );
function offers_supllier_affiliate_meta_field_edit($term) {
	// put the term ID into a variable
	$t_id = $term->term_id;
 
	// retrieve the existing value(s) for this meta field. This returns an array
	$term_meta = get_option( "taxonomy_$t_id" ); ?>
	<tr class="form-field">
	<th scope="row" valign="top"><label for="term_meta[offers_supllier_affiliate_url]"><?php _e( 'Affiliate url', '88offers' ); ?></label></th>
		<td>
			<input type="text" name="term_meta[offers_supllier_affiliate_url]" id="term_meta[offers_supllier_affiliate_url]" value="<?php echo esc_attr( $term_meta['offers_supllier_affiliate_url'] ) ? esc_attr( $term_meta['offers_supllier_affiliate_url'] ) : ''; ?>">
			<p class="description"><?php _e( 'Nhập affiliate url','88offers' ); ?></p>
		</td>
	</tr>
<?php
}
add_action( 'suppliers_edit_form_fields', 'offers_supllier_affiliate_meta_field_edit', 10, 2 );

Cơ bản là như vậy còn phần bên trong thân các hàm thì bạn tuỳ chỉnh thêm bao nhiêu trường meta theo ý mình thì tuỳ nhu cầu của bạn.

Good luck to you!!!

Nguồn bài viết: https://pippinsplugins.com/adding-custom-meta-fields-to-taxonomies/ (thanks so much!)

2 thoughts on “Thêm custom meta fields cho taxonomies trong WordPress”

Leave a Comment

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Website này sử dụng Akismet để hạn chế spam. Tìm hiểu bình luận của bạn được duyệt như thế nào.