Have you ever needed a WordPress advertising plugin for your website? In this tutorial series we will create a simple WordPress advertising plugin to manage your own ads. Before we can even display them, we need to create them in our admin area.
This tutorial is a part of a tutorial series “Simple WordPress Advertising Plugin“. The series consists of several tutorials which are:
To follow this tutorial I would advise you to have WordPress installed on your local machine and also to have an editor of choice for writing code. You are also free to follow this tutorial without writing a single line of code.
Some of the code will be the same as the one we were writing in some of the previous articles. I will link to those articles alongside the big chunk of code which you can easily copy.
You can also download the entire code from this article here: Code for this article.
This plugin will be a simple one where a user can define the name of the Ad, some Ad information and also add an image for the Ad.
For now, let’s create a folder simple-wp-ads inside the WordPress plugin folder wp-content/plugins. Inside that folder create a file with the same name simple-wp-ads.php. In this file, we will add the required plugin’s information and also define some constants.
The constants SWA_ROOT and SWA_URI will be used when including other files from this plugin or when enqueueing some assets from this plugin. We will also create a class that will hold and create everything we need in this plugin.
This class will use the Singleton pattern which means that it will always be instantiated as one object. This will allow our object to contain every information we need through the whole life cycle of our page.
Now, when calling the static method instance() we will always get only one object of that class which will persist in the RAM memory of our server.
The method load_dependencies() will be populated through this article and also others to come. Before we begin populating it, let’s start our Simple_WP_Ads object. We will instantiate it and use it after WordPress loads all other plugins.
The first function swa() is a helper function that we will use to get the single instance of our object. The function swa_start() instantiates or gets our object and also to call all the methods we need to load for our plugin to function properly.
For now the only method we will call is the load_dependencies() method.
We will use the WordPress custom post type to manage our ads. We will use the title and the featured image of that post type. We will add other information through a metabox.
We will also have our file that will hold post type information in a folder called inc. So create a new folder in our plugin that is called inc. Inside that folder add a file swa-cpt.php. This file will hold everything we need to start our post type.
We can now add that dependancy to our main class:
The method load_dependencies() will now load our file when our plugin starts. To create a custom post type in the admin area we also need to add the post type definition. So add this to that new file:
To handle the Ads we will use another status. This status will be used to define if the Ad has ended. To be able to easily add it, I will use the code we have created in the article How to Create Custom WordPress Post Statuses. The class name has changed to follow this plugin but everything else is the same. Since this is a rather long class, I will only give you the link here so you can easily copy it from there: SWA_Status.
This code will be placed in a new file class-swa-status.php. Save this file inside the inc folder. Since it will only hold the class definition, we will have to create another file to hold the instantiated object. Create a new file swa-statuses.php, again in the folder inc.
Let’s add them as our dependencies:
Now, when our plugin has started, both files will be required. The only thing left is to create the status. Copy this code inside the file swa-statuses.php:
The status ended will be assigned to an Ad that has expired. We will do that using the WordPress cron, but that is a part of another article.
So, now we have our Ad post type and our new custom status. The only thing left for this article is to create a metabox that will hold other Ad information.
This is the last part of this tutorial. Thank you for reading this far!
We will start by having only one input and that will be the duration of our Ad. This will be a simple text input.
To create our metabox, we will use the same steps as described in my other article: How to Create a Custom WordPress Metabox with OOP. We will use both the abstract class and also the metabox class.
If you don’t have time to read it, then you can get the codes here: Abstract Settings Class and Metabox Class. Create a new folder inside the inc folder and name it abstracts. Now create a new file class-swa-settings.php and copy the code for abstract settings class.
Create another file class-swa-metabox.php but this time in the folder inc (not inside the abstracts) and copy the code for metabox class.
The metabox class is and extended class from the abstract settings class and it will be used to easily create metaboxes.
We will now load those files as our dependencies:
We are loading the metabox class only when we are inside the admin area. The other thing that we did here is to hook another method from our main class to construct metaboxes only when we are on a new or edit post screen.
Our new method construct_metabox_on_ad will load another file that will hold metabox definitions. Now, let’s create the file swa-metaboxes.php inside the folder inc and add this code:
Here we have defined a new metabox for the custom post type swa. We have also added a new field to it with the name duration. This will hold the duration of our Ad.
This was an introduction to our Simple WordPress Advertising Plugin. In the next tutorial we will see how to use the post status, duration and also how to show the ad on our website. We will also cache them on a daily basis so that our website response is much faster.
If you have any experience with handling Ads or want to suggest a great WordPress Ad Plugin, you are welcome to comment it below:)
Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.