To add three custom fields to the category add and edit forms, you can modify the existing code as follows:

// Add custom fields to category add form
function add_category_custom_fields() {
    ?>
    <div class="form-field">
        <label for="custom_field_1"><?php _e('Custom Field 1', 'textdomain'); ?></label>
        <input type="text" name="custom_field_1" id="custom_field_1" value="" />
        <p class="description"><?php _e('Enter a value for Custom Field 1', 'textdomain'); ?></p>
    </div>
    <div class="form-field">
        <label for="custom_field_2"><?php _e('Custom Field 2', 'textdomain'); ?></label>
        <input type="text" name="custom_field_2" id="custom_field_2" value="" />
        <p class="description"><?php _e('Enter a value for Custom Field 2', 'textdomain'); ?></p>
    </div>
    <div class="form-field">
        <label for="custom_field_3"><?php _e('Custom Field 3', 'textdomain'); ?></label>
        <input type="text" name="custom_field_3" id="custom_field_3" value="" />
        <p class="description"><?php _e('Enter a value for Custom Field 3', 'textdomain'); ?></p>
    </div>
    <?php
}
add_action('category_add_form_fields', 'add_category_custom_fields', 10, 2);

// Add custom fields to category edit form
function edit_category_custom_fields($tag) {
    $custom_field_1 = get_term_meta($tag->term_id, 'custom_field_1', true);
    $custom_field_2 = get_term_meta($tag->term_id, 'custom_field_2', true);
    $custom_field_3 = get_term_meta($tag->term_id, 'custom_field_3', true);
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="custom_field_1"><?php _e('Custom Field 1', 'textdomain'); ?></label></th>
        <td>
            <input type="text" name="custom_field_1" id="custom_field_1" value="<?php echo esc_attr($custom_field_1); ?>" />
            <p class="description"><?php _e('Enter a value for Custom Field 1', 'textdomain'); ?></p>
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="custom_field_2"><?php _e('Custom Field 2', 'textdomain'); ?></label></th>
        <td>
            <input type="text" name="custom_field_2" id="custom_field_2" value="<?php echo esc_attr($custom_field_2); ?>" />
            <p class="description"><?php _e('Enter a value for Custom Field 2', 'textdomain'); ?></p>
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="custom_field_3"><?php _e('Custom Field 3', 'textdomain'); ?></label></th>
        <td>
            <input type="text" name="custom_field_3" id="custom_field_3" value="<?php echo esc_attr($custom_field_3); ?>" />
            <p class="description"><?php _e('Enter a value for Custom Field 3', 'textdomain'); ?></p>
        </td>
    </tr>
    <?php
}
add_action('category_edit_form_fields', 'edit_category_custom_fields', 10, 2);

// Save custom field data
function save_category_custom_fields($term_id) {
    if (isset($_POST['custom_field_1'])) {
        update_term_meta($term_id, 'custom_field_1', sanitize_text_field($_POST['custom_field_1']));
    }
    if (isset($_POST['custom_field_2'])) {
        update_term_meta($term_id, 'custom_field_2', sanitize_text_field($_POST['custom_field_2']));
    }
    if (isset($_POST['custom_field_3'])) {
        update_term_meta($term_id, 'custom_field_3', sanitize_text_field($_POST['custom_field_3']));
    }
}
add_action('created_category', 'save_category_custom_fields', 10, 2);
add_action('edited_category', 'save_category_custom_fields', 10, 2);

This code will add three custom fields (custom_field_1, custom_field_2, custom_field_3) to the category add and edit forms, and save their values when the category is created or updated.

Add custom field to category add formfunction add_category_custom_field div class=form-field label for=custom_fieldphp _eCustom Field textdomain; label input type=text name=custo

原文地址: https://www.cveoy.top/t/topic/i34D 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录