0
0
WordpressHow-ToBeginner · 4 min read

How to Add Custom Field to Product in WordPress Easily

To add a custom field to a WordPress product, use the add_post_meta function or update it with update_post_meta targeting the product's post ID. You can also add custom fields via WooCommerce hooks like woocommerce_product_options_general_product_data to show fields in the admin product edit page.
📐

Syntax

Use add_post_meta($post_id, $meta_key, $meta_value, $unique) to add a new custom field to a product. Use update_post_meta($post_id, $meta_key, $meta_value) to update or add if it doesn't exist.

  • $post_id: The product's ID.
  • $meta_key: The name of your custom field.
  • $meta_value: The value you want to save.
  • $unique: (optional) true to prevent duplicates.

To show custom fields in the WooCommerce admin product page, hook into woocommerce_product_options_general_product_data and use woocommerce_wp_text_input or similar functions.

php
add_post_meta(123, 'custom_field_name', 'custom value', true);

update_post_meta(123, 'custom_field_name', 'new value');

// Hook to add field in admin
add_action('woocommerce_product_options_general_product_data', function() {
    woocommerce_wp_text_input(array(
        'id' => 'custom_field_name',
        'label' => __('Custom Field', 'woocommerce'),
        'desc_tip' => true,
        'description' => __('Enter your custom value here.', 'woocommerce')
    ));
});
💻

Example

This example adds a custom text field to the WooCommerce product edit page and saves its value when the product is saved.

php
<?php
// Add custom field to product general tab
add_action('woocommerce_product_options_general_product_data', function() {
    woocommerce_wp_text_input(array(
        'id' => 'custom_product_text_field',
        'label' => __('Custom Text Field', 'woocommerce'),
        'desc_tip' => true,
        'description' => __('Enter a custom text value for this product.', 'woocommerce')
    ));
});

// Save custom field value
add_action('woocommerce_process_product_meta', function($post_id) {
    $custom_field_value = isset($_POST['custom_product_text_field']) ? sanitize_text_field($_POST['custom_product_text_field']) : '';
    update_post_meta($post_id, 'custom_product_text_field', $custom_field_value);
});
Output
In the WordPress admin product edit page, a new text input labeled 'Custom Text Field' appears under the General tab. When saved, the value is stored as product meta with key 'custom_product_text_field'.
⚠️

Common Pitfalls

  • Not sanitizing input before saving can cause security issues.
  • Forgetting to hook into woocommerce_process_product_meta means the custom field won't save.
  • Using wrong meta keys or IDs causes the field not to show or save properly.
  • Not checking if the field exists before updating can cause duplicate meta entries.
php
<?php
// Wrong: Missing save hook
add_action('woocommerce_product_options_general_product_data', function() {
    woocommerce_wp_text_input(array('id' => 'custom_field'));
});

// Right: Add save hook
add_action('woocommerce_process_product_meta', function($post_id) {
    if (isset($_POST['custom_field'])) {
        update_post_meta($post_id, 'custom_field', sanitize_text_field($_POST['custom_field']));
    }
});
📊

Quick Reference

  • add_post_meta: Adds a new custom field to a product.
  • update_post_meta: Updates or adds a custom field.
  • woocommerce_product_options_general_product_data: Hook to add fields in product admin.
  • woocommerce_process_product_meta: Hook to save custom field data.
  • Always sanitize input with sanitize_text_field or similar.

Key Takeaways

Use add_post_meta or update_post_meta to add custom fields to products by product ID.
Hook into woocommerce_product_options_general_product_data to display custom fields in admin.
Save custom field data using woocommerce_process_product_meta hook with proper sanitization.
Always sanitize user input to keep data safe and clean.
Check your meta keys and hook usage carefully to avoid common mistakes.