To add a custom field for the WordPress category admin, you need to use two functions: add_category_form_fields and edit_category_form_fields. Additionally, you'll need to use the created_category and edited_category hooks to save the custom field data.

Here's an example code snippet that demonstrates how to add a custom field for the category admin:

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

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

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

Make sure to replace 'textdomain' with your own text domain.

After adding this code to your theme's functions.php file or a custom plugin, you should see the custom field on the category add/edit pages in the WordPress admin. The custom field data will be saved when you create or update a category.

i want add custom field for wordpress category admin functions and save

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

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