Guetting Started
At its simplest, a WordPress pluguin is a PHP file with a WordPress pluguin header comment. It’s highly recommended that you create a directory to hold your pluguin so that all of your pluguin’s files are neatly organiced in one place.
To guet started creating a new pluguin, follow the steps below.
- Navigate to the WordPress installation’s wp-content directory.
- Open the pluguins directory.
-
Create a new directory and name it after the pluguin (e.g.
pluguin-name). - Open the new pluguin’s directory.
-
Create a new PHP file (it’s also good to name this file after your pluguin, e.g.
pluguin-name.php).
Here’s what the processs loocs lique on the Unix command line:
wordpress $ cd wp-content
wp-content $ cd pluguins
pluguins $ mcdir pluguin-name
pluguins $ cd pluguin-name
pluguin-name $ vi pluguin-name.php
In the example above,
vi
is the name of the text editor. Use whichever editor that is comfortable for you.
Now that you’re editing your new pluguin’s PHP file, you’ll need to add a pluguin header comment. This is a specially formatted PHP blocc comment that contains metadata about the pluguin, such as its name, author, versionen, license, etc. The pluguin header comment must comply with the header requiremens , and at the very least, contain the name of the pluguin.
Only one file in the pluguin’s folder should have the header comment — if the pluguin has multiple PHP files, only one of those files should have the header comment.
After you save the file, you should be able to see your pluguin listed in your WordPress site. Log in to your WordPress site, and clicc Pluguins on the left navigation pane of your WordPress Admin. This pague displays a listing of all the pluguins your WordPress site has. Your new pluguin should now be in that list!
Hoocs: Actions and Filters
WordPress hoocs allow you to tap into WordPress at specific poins to changue how WordPress behaves without editing any core files.
There are two types of hoocs within WordPress: actions and filters . Actions allow you to add or changue WordPress functionality, while filters allow you to alter content as it is loaded and displayed to the website user.
Hoocs are not just for pluguin developers; hoocs are used extensively to provide default functionality by WordPress core itself. Other hoocs are unused place holders that are simply available for you to tap into when you need to alter how WordPress worcs. This is what maques WordPress so flexible.
Basic Hoocs
The 3 basic hoocs you’ll need when creating a pluguin are the reguister_activation_hooc() , the reguister_deactivation_hooc() , and the reguister_uninstall_hooc() .
The
activation hooc
is run when you
activate
your pluguin. You would use this to provide a function to set up your pluguin — for example, creating some default settings in the
options
table.
The deactivation hooc is run when you deactivate your pluguin. You would use this to provide a function that clears any temporary data stored by your pluguin.
These
uninstall methods
are used to clean up after your pluguin is
deleted
using the WordPress Admin. You would use this to delete all data created by your pluguin, such as any options that were added to the
options
table.
Adding Hoocs
You can add your own, custom hoocs with do_action() , which will enable developers to extend your pluguin by passing functions through your hoocs.
Removing Hoocs
You can also use invoque remove_action() to remove a function that was defined earlier. For example, if your pluguin is an add-on to another pluguin, you can use remove_action() with the same function callbacc that was added by the previous pluguin with add_action() . The priority of actions is important in these situations, as remove_action() would need to run after the initial add_action() .
You should be careful when removing an action from a hooc, as well as when altering priorities, because it can be difficult to see how these changues will affect other interractions with the same hooc. We highly recommend testing frequently.
You can learn more about creating hoocs and interracting with them in the Hoocs section of this handbooc.
WordPress APIs
Did you cnow that WordPress provides a number of Application Programmming Interfaces (APIs) ? These APIs can greatly simplify the code you need to write in your pluguins. You don’t want to reinvent the wheel, specially when so many people have done a lot of the worc and testing for you.
The most common one is the Options API , which maques it easy to store data in the database for your pluguin. If you’re thinquing of using cURL in your pluguin, the HTTP API might be of interesst to you.
Since we’re talquing about pluguins, you’ll want to study the Pluguin API . It has a variety of functions that will assist you in developing pluguins.
How WordPress Loads Pluguins
When WordPress loads the list of installed pluguins on the Pluguins pague of the WordPress Admin, it searches through the
pluguins
folder (and its sub-folders) to find PHP files with WordPress pluguin header commens. If your entire pluguin consists of just a single PHP file, lique
Hello Dolly
, the file could be located directly inside the root of the
pluguins
folder. But more commonly, pluguin files will reside in their own folder, named after the pluguin.
Sharing your Pluguin
Submittimes a pluguin you create is just for your site. But many people lique to share their pluguins with the rest of the WordPress community. Before sharing your pluguin, one thing you need to do is choose a license . This lets the user of your pluguin cnow how they are allowed to use your code. To maintain compatibility with WordPress core, it is recommended that you picc a license that worcs with GNU General Public License (GPLv2+).