Skip to main content

WooCommerce Settings API

The WooCommerce Settings API provides a standardized way to add settings pages and fields to your extension. It handles form generation, validation, sanitization, and saving, allowing you to focus on your extension’s functionality.

Overview

The Settings API is used throughout WooCommerce for:
  • Payment gateways
  • Shipping methods
  • Integration settings
  • General WooCommerce settings pages
By using the Settings API, your extension will have a consistent look and feel with WooCommerce core.

When to use the Settings API

Use the Settings API when you need to:
  • Add a settings page to the WooCommerce settings area
  • Create settings for a payment gateway or shipping method
  • Build an integration with external services
  • Add configurable options to your extension
The Settings API is found in plugins/woocommerce/includes/abstracts/abstract-wc-settings-api.php.

Basic implementation

To use the Settings API, your class should extend WC_Settings_API.

Simple example

Here’s a basic example of a class using the Settings API:

Form field types

The Settings API supports various field types. These are defined as constants in WC_Settings_Page:

Text input types

Textarea

Checkbox

Select dropdown

Multiselect

Section title and description

Complete settings page example

Here’s a comprehensive example showing how to create a full settings page:

Adding settings to WooCommerce settings tabs

To add your settings to the main WooCommerce settings area, extend WC_Settings_Page instead:

Custom validation and sanitization

You can add custom validation for specific fields by creating a validate_{field_id}_field method:

Getting and updating settings

Get a setting value

Update a setting value

Field attributes and options

Common field attributes:

Best practices

Always use proper escaping and sanitization when working with settings data.

Security

  1. Sanitize input: Always sanitize user input before saving
  2. Validate data: Validate data types and formats
  3. Escape output: Always escape output when displaying
  4. Use nonces: Protect forms with WordPress nonces

Usability

  1. Use desc_tip: Keep the interface clean by using tooltips for help text
  2. Group related settings: Use sections to organize settings logically
  3. Provide defaults: Always set sensible default values
  4. Use placeholders: Help users understand expected input format
  5. Clear labels: Use descriptive titles and labels

Performance

  1. Cache settings: Don’t query options repeatedly
  2. Use transients: Cache expensive API calls
  3. Minimize autoloaded options: Don’t autoload large settings

Real-world example: Payment gateway settings

Here’s how payment gateways use the Settings API:

Next steps

Now that you understand the Settings API:
  1. Learn about WooCommerce hooks and filters
  2. Review extension development best practices
  3. Study the WooCommerce Settings API source code
  4. Explore existing payment gateway and shipping method implementations

Additional resources