We use affiliate links. If you purchase something using one of these links, we may receive compensation or commission.
Hi all, and welcome to my short tutorial on how to code a useful WordPress plugin in just 10 minutes.
We’re going to create a functional, minimal, and optimized plugin that automatically places an affiliate disclosure/disclaimer at the top of the posts on our blogs – all in about 10 minutes.
Updated code from circa 2012/2013. (#FrostburgYears!)
Use at your own risk/reward. Seek legal counsel for appropriate disclosure and dislaimer messages. This is a template plugin, so feel free to slice, dice, and diy.
Here’s what you’ll need to get started:
- A WordPress website.
- A code editor (I prefer Notepad++, MS Code, and/or Sublime).
- Access to upload your finished plugin.
- A great attitude!
Ok, let’s jump right into the code. This plug-in will only be one file, since it’s designed for functional use.
In the case of production plugins, you’d also include a readme.txt, and a more complex file structure if your plugin requires multiple functions. However, since this plugin has a very specific purpose, and we want this tutorial to be for beginners, we’re just going to roll with one main file.
First, open your code editor, and start with the following code. WordPress plugins are usually coded in php (although that appears to be shifting a little bit in the new js-driven era), so you’ll start with the opening <?php tag.
You’ll notice that there are many elements here, which are all either informative or intelligent elements that help end users understand details about your plugin, as well as populate fields in your WordPress backend.
We’re going to call our plugin Affy (cute, right?), but feel free to change the rest of the fields to information about you!
*Note, WordPress recommends licensing plugins under the GPL tree for compatibility.
Now it’s time to write our function! I’ve provided the standard disclaimer we use on my brand blogs, but feel free to alter to your needs, and run past your team to ensure proper compliance.
Not too bad right? Well, there’s one more thing you’ll need to do, which is common in WordPress plugins: create an output, or apply a filter.
In this case, we’re going to apply a filter to tap into WordPress’ predefined hooks so that we don’t have to manually edit posts with a shortcode or edit core template files.
Filters are pretty easy. Let’s try this one:
Okay, now we actually have a functioning plugin! But, let’s do one more thing. We don’t want the disclaimer to show up on all pages, just posts, so let’s add a conditional.
Check the post content type, by adding a new conditional to the function. Check out our update to the code we created in Step 2:
Okay, that’s it! I’m so proud of you! I’ve included the full code below (+ some 2020 additions), and invite your thoughts, comments, and feedback below!
✨ New for 2020! Updated w/ admin panel and released on WP repo below.
Thanks for learning with me!