Create a custom field on the WooCommerce Category Admin page

function add_custom_category_image() {

  ?>

  <div class=”form-field”>

    <label for=”category_image”><?php esc_html_e(‘Category Image’, ‘textdomain’); ?></label>

    <input type=”text” name=”category_image” id=”category_image” value=””>

    <p class=”description”><?php esc_html_e(‘Enter category image URL for this category.’, ‘textdomain’); ?></p>

  </div>

  <?php

}

add_action(‘product_cat_add_form_fields’, ‘add_custom_category_image’, 10, 2);

// Save custom category image data when creating or updating a category

function save_custom_category_image($term_id) {

  if (isset($_POST[‘category_image’])) {

    update_term_meta($term_id, ‘category_image’, sanitize_text_field($_POST[‘category_image’]));

  }

}

add_action(‘created_product_cat’, ‘save_custom_category_image’, 10, 2);

add_action(‘edited_product_cat’, ‘save_custom_category_image’, 10, 2);

// Add custom category image field to product categories edit page

function add_custom_category_image_edit_page($term) {

  $category_image = get_term_meta($term->term_id, ‘category_image’, true);

  ?>

  <tr class=”form-field”>

    <th scope=”row” valign=”top”>

      <label for=”category_image”><?php esc_html_e(‘Category Image’, ‘textdomain’); ?></label>

    </th>

    <td>

      <input type=”text” name=”category_image” id=”category_image” value=”<?php echo esc_attr($category_image); ?>”>

      <p class=”description”><?php esc_html_e(‘Enter category image URL for this category.’, ‘textdomain’); ?></p>

    </td>

  </tr>

  <?php

}

add_action(‘product_cat_edit_form_fields’, ‘add_custom_category_image_edit_page’);

// Save custom category image data when updating a category

function save_custom_category_image_edit_page($term_id) {

  if (isset($_POST[‘category_image’])) {

    update_term_meta($term_id, ‘category_image’, sanitize_text_field($_POST[‘category_image’]));

  }

}

add_action(‘edited_product_cat’, ‘save_custom_category_image_edit_page’, 10, 2);

// Expose custom category image in REST API

function expose_custom_category_image() {

  register_rest_field(‘product_cat’, ‘category_image’, array(

    ‘get_callback’  => ‘get_custom_category_image’,

    ‘update_callback’ => ‘update_custom_category_image’,

    ‘schema’     => null,

  ));

}

add_action(‘rest_api_init’, ‘expose_custom_category_image’);

// Callback function to get custom category image value

function get_custom_category_image($category) {

  return get_term_meta($category[‘id’], ‘category_image’, true);

}

// Callback function to update custom category image value

function update_custom_category_image($value, $category) {

  if (isset($value)) {

    update_term_meta($category->term_id, ‘category_image’, sanitize_text_field($value));

  }

}

This code includes creating the custom field in the category for adding data when a category is created and updated using the REST API as well as the category innner page

Leave a comment

Your email address will not be published. Required fields are marked *