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 ofwoocommerce-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
Check if WooCommerce is active
Your extension should only initialize if WooCommerce is active. Add this check after the header:woocommerce-product-badge.php
Define constants
Define useful constants for your plugin:woocommerce-product-badge.php
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 separateuninstall.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 atassets/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 completewoocommerce-product-badge.php file:
View complete code
View complete code
woocommerce-product-badge.php
Next steps
Now that you’ve built your first WooCommerce extension:- Review extension development best practices
- Learn about the WooCommerce Settings API to add settings pages
- Explore WooCommerce hooks and filters for more customization options
- Add a changelog file to track your changes
- Study deactivation and uninstallation best practices