Skip to main content

Building your first WooCommerce extension

This tutorial will guide you through creating a simple WooCommerce extension from scratch. You’ll learn the essential structure, plugin headers, and activation/deactivation hooks needed for any WooCommerce extension.

What we’ll build

We’ll create a simple extension called “WooCommerce Product Badge” that adds a custom badge to products on sale. This extension will demonstrate:
  • Proper plugin structure and file organization
  • WordPress plugin headers
  • WooCommerce dependency checking
  • Activation and deactivation hooks
  • Using WooCommerce template hooks

Plugin structure

First, let’s create the basic file structure for our extension.
1

Create the plugin directory

Create a new directory in wp-content/plugins/ called woocommerce-product-badge:
2

Create the main plugin file

Create a file named woocommerce-product-badge.php. The main plugin file should match the directory name.
The text domain should match your plugin directory name. For example, a plugin with directory name woocommerce-product-badge should have text domain woocommerce-product-badge. Do not use underscores.
3

Create additional files

Create a basic file structure:

Plugin header comment

Every WordPress plugin needs a header comment in the main file. This provides WordPress with metadata about your plugin. Add this to the top of woocommerce-product-badge.php:
woocommerce-product-badge.php

Header field explanations

  • Plugin Name: The name displayed in the WordPress admin
  • Plugin URI: The plugin’s homepage or product page
  • Description: A short description of what the plugin does
  • Version: Current version number (use semantic versioning)
  • Author: The plugin author’s name
  • Author URI: The author’s website
  • Developer: Developer name if different from author
  • Developer URI: Developer’s website
  • Text Domain: Used for internationalization (must match directory name)
  • Domain Path: Location of translation files
  • WC requires at least: Minimum WooCommerce version required
  • WC tested up to: Latest WooCommerce version tested with
  • License: Plugin license
  • License URI: Full license text URL
The defined( 'ABSPATH' ) || exit; line prevents direct access to your PHP files, which is crucial for security. Include this in all your PHP files.

Check if WooCommerce is active

Your extension should only initialize if WooCommerce is active. Add this check after the header:
woocommerce-product-badge.php
For multisite installations, you may need additional checks. See Check if WooCommerce is active for more details.

Define constants

Define useful constants for your plugin:
woocommerce-product-badge.php
These constants make it easy to reference your plugin’s directory, URL, and version throughout your code.

Create the main plugin class

Organize your code in a class for better structure and maintainability:
woocommerce-product-badge.php

Activation and deactivation hooks

Plugins can perform actions when activated or deactivated. Add these hooks at the end of your main plugin file:
woocommerce-product-badge.php
Use the activation hook to set up initial options, create custom database tables, or check dependencies. Use the deactivation hook to clean up temporary data, but don’t delete user options—save that for the uninstall hook.

Uninstallation

For proper cleanup when a user uninstalls your plugin, create a separate uninstall.php file:
uninstall.php
Only delete data in uninstall.php, never in the deactivation hook. Users often deactivate plugins temporarily without wanting to lose their settings.

Add some styling

Create a CSS file at assets/css/product-badge.css:
assets/css/product-badge.css

Testing your extension

1

Activate the plugin

Go to Plugins in your WordPress admin and activate “WooCommerce Product Badge”.
2

Create test products

Create a few products and set some of them on sale by adding a sale price.
3

View the shop page

Visit your shop page to see the custom sale badges on products that are on sale.
4

Test deactivation

Deactivate the plugin and verify that the badges are removed and no errors occur.

Complete file listing

Here’s the complete woocommerce-product-badge.php file:
woocommerce-product-badge.php

Next steps

Now that you’ve built your first WooCommerce extension:
  1. Review extension development best practices
  2. Learn about the WooCommerce Settings API to add settings pages
  3. Explore WooCommerce hooks and filters for more customization options
  4. Add a changelog file to track your changes
  5. Study deactivation and uninstallation best practices

Additional resources