Trao đổi về dự án
Wordpress Backoffice

WordPress: Thêm một phân loại theo cách thủ công

Ví dụ về việc thêm thuế quan thủ công (không thông qua cấu hình).Đặt trong tệp functions.php: /** * Manual add new Field to taxonomy or you can use plugin "taxonomy manager" * * BEGIN */ // A callback function to add a custom field to our "expertise" taxon...

WordPress: Thêm một phân loại theo cách thủ công



Ví dụ về việc thêm thuế quan thủ công (không thông qua cấu hình).

Đặt trong tệp functions.php:

/**
* Manual add new Field to taxonomy or you can use plugin "taxonomy manager"
*
* BEGIN
*/
// A callback function to add a custom field to our "expertise" taxonomy
function expertise_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option("taxonomy_term_$t_id"); // Do the check
?>

<tr class="form-field">
<th scope="row" valign="top">
<label for="presenter_id"><?php _e('WordPress User ID'); ?></label>
</th>
<td>
<input type="text" name="term_meta[presenter_id]" id="term_meta[presenter_id]" size="25" style="width:60%;" value="<?php echo $term_meta['presenter_id'] ? $term_meta['presenter_id'] : ''; ?>"><br />
<span class="description"><?php _e('The Presenter\'s WordPress User ID'); ?></span>
</td>
</tr>

<?php
}

// A callback function to save our extra taxonomy field(s)
function save_taxonomy_custom_fields($term_id) {
if (isset($_POST['term_meta'])) {
$t_id = $term_id;
$term_meta = get_option("taxonomy_term_$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_term_$t_id", $term_meta);
}
}

Sao chép sự tùy chỉnh này hôm nay

Nguyên tắc của hook vẫn hữu ích, nhưng các màn hình quản trị và API của WordPress đã phát triển kể từ khi bài viết được xuất bản. Hãy thử mã trong một môi trường thử nghiệm với phiên bản chính xác của lõi, giao diện và các tiện ích mở rộng của trang web.

  • Sao lưu các tệp và cơ sở dữ liệu trước bất kỳ sửa đổi nào.
  • Kiểm soát quyền hạn người dùng, nonce, xác thực đầu vào và thoát đầu ra.
  • Đặt phần tùy chỉnh vào một tiện ích mở rộng riêng hoặc một chủ đề con để bảo toàn các bản cập nhật.

Tham chiếu : sách hướng dẫn chính thức phát triển các tiện ích mở rộng WordPress.

Chia sẻ bài viết