Adding WooCommerce settings

Instead of writing up another ‘how to’ post explaining what every filter does, I decided to just share a class I’ve created that can be reused for adding your own settings to WooCommerce with settings tab to the WooCommerce settings page.

Let’s start by sharing the class and then I’ll explain what you should change and how you should integrate it in your WooCommerce extension.

As you might have noticed there are a couple of values that you need to change in this class in order for it to work in your WooCommerce extension.

What you should change

There are only a few things you need to change in the above class to implement it in your extension.

Change the class name

It’s important to change the class name so it matches your extension prefix. Not doing so might cause conflicts with other extensions. The class is defined here:

class WC_BK_Settings {

Change the namespace

Change the following line to a unique string that identifies your WooCommerce Extension, I recommend using your extension name.


Change the text domain to your text domain

The class uses ‘example-text-domain’ at the moment, change this to your own text domain. I suggest to do a search and replace in the IDE of your choice. Here’s an example text domain line that should be changed:

'name' => __( 'Your section title', 'example-text-domain' ),

Change the tab label

Because this string should be translatable, we can’t use a variable and have to pass it as a ‘real’ string. Change the following line to a tab label of your choice.

$settings_tabs[self::SETTINGS_NAMESPACE] = __( 'Your Label', 'example-text-domain' );

Implementing the class in your extension

Implementing this settings class in your WooCommerce extension is pretty easy. All you need to do is add the following code to your extension:

// Only load in admin
if ( is_admin() ) {
	// Initiate the settings class
	$settings = new WC_BK_Settings();
	// Setup the hooks and filters

Get option via the class

Instead of using the function ‘get_option’, use the class method ‘get_option’. This will automatically set the correct default and make the options filterable. Here’s an example getting the value of the ‘example_input’ option.

$example_input = $settings->get_option( 'example_input' );

That’s it, the class will handle everything else. I suggest adding above code to a place that’s hooked rather than directly in your plugin file. I normally initiate my plugins in ‘plugins_loaded’.

I hope this helps any one out reading this. Missing something? Found a problem? Got an improvement? Please let me know in the comments below.

Related Posts

Powered By Related Posts for WordPress
Click Here to Learn More About Related Posts for WordPress

4 thoughts on “Adding WooCommerce settings

  1. Great tutorial Barry Thanks for sharing!

  2. Neat! It’d be cool to have this extend the WC_Settings_Page class 🙂

Leave a Reply

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