To get started, we need to create the plugin files. Plugins are typically stored in the WordPress filesystem under wp-content/plugins/{your-plugin}.
There are two ways of creating plugin files:
- Use a single plugin file that contains the entire plugin.
- Use a folder with multiple plugin files.
Almost all modern plugins use multiple files, so we're going to create a folder for our plugin. Follow the steps given here:
-
Under /wp-content/plugins/, add a folder for your plugin. I'm going to call mine woocommerce-example-plugin:
- Navigate inside of that folder and create a file with the same name. I'll call mine woocommerce-example-plugin.php. This will be the main file for the plugin.
- Open up your main plugin file in a code editor such as Sublime Text, Atom, or Notepad ++.
- Add the following plugin header information (https://developer.wordpress.org/plugins/plugin-basics/header-requirements/). This information is displayed in your WordPress backend:
<?php
/*
Plugin Name: WooCommerce Example Plugin
Plugin URI: http://speakinginbytes.com
Description: A wrapper plugin for our custom WooCommerce
functionality
Version: 1.0
Author: Patrick Rauland
Author URI: http://speakinginbytes.com
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: woocommerce-example-plugin
Domain Path: /languages
*/
- Save the file.
If we stopped right now we'd have a completely valid plugin that would show up in the backend of WordPress, but it wouldn't do anything. Let's program our plugin so it only runs when WooCommerce is active.