Monday, March 13, 2023
HomePHPEasy methods to Create a Customized WordPress Plugin From Scratch

Easy methods to Create a Customized WordPress Plugin From Scratch

Plugins add additional performance to your WordPress web site over and above what comes with WordPress core. All the things from a reserving calendar or animated slider to a full-featured studying administration system or on-line market—you’ll be able to add all of them to your web site with plugins. 

On this information, I’ll present you how you can create your personal WordPress plugin. I’ll present you how you can use greatest observe in plugin improvement, how you can get the code in your plugin to run, and how you can construction your plugin’s code and information. I’ll additionally stroll you thru the method of making your first plugin and reply some FAQs.

Let’s begin with this WordPress plugin tutorial:

Free and Paid WordPress Plugins

If you wish to add some particular options to your web site, there are many locations you should buy or obtain plugins. The WordPress plugin listing contains hundreds of free plugins that’ll enable you create the location you want. However in case you’re in search of extra superior options, a greater person interface, or improved assist, it’s value shopping for premium plugins from authors on CodeCanyon.

CodeCanyon WordPress PluginsCodeCanyon WordPress PluginsCodeCanyon WordPress Plugins

However generally you may must code your personal plugin! This may be extra environment friendly than utilizing a third-party plugin, as you may solely want part of the code supplied by these. It additionally means you’ll be able to develop a plugin that meets your wants extra exactly, or you’ll be able to customise an present plugin to adapt it in your web site.

What Do You Have to Make a Plugin?

To construct your personal plugin and run it in your WordPress web site, you’ll want:

  • a code editor
  • a improvement WordPress set up with a duplicate of your reside web site for testing

Do not check your plugin in your reside web site till you understand it really works!

Should you don’t have already got an area WordPress set up, comply with our information to copying your web site to an area set up. Or in case you can’t set up WordPress regionally, use a reproduction of your web site on a testing set up in your server. Discover out how you can copy your web site.

Sorts of WordPress Plugin

Plugins can perform a lot of duties. What all of them have in frequent is that they add additional performance to your web site. Sorts of WordPress plugin embody:

  • web site upkeep plugins for issues like safety, efficiency, or backups
  • advertising and marketing and gross sales plugins for issues like website positioning, social media, or eCommerce
  • content material plugins reminiscent of {custom} put up sorts, widgets, shortcodes, kinds, galleries, and video feeds
  • API plugins that work with the WordPress REST API or pull in exterior content material from companies like Google Maps
  • group plugins that add social networking options

… and plenty extra! For an concept of what plugins can do, take a look at the WordPress plugin listing and the CodeCanyon market.

WordPress plugin directoryWordPress plugin directoryWordPress plugin directory
When you create a plugin for WordPress, customers will be capable of discover it on the WordPress plugin web page.

How To Create a WordPress Plugin? 

Earlier than you get began constructing your {custom} plugin, it’s value figuring out all it takes to create a {custom} plugin for WordPress. Precisely what the plugin code will appear like will rely in your plugin: some are small, with only one plugin file, whereas others are large, with a number of embody information, scripts, stylesheets, and template information. And there are a lot that fall someplace within the center.

The weather you’ll most likely have in your WordPress plugin are:

  • the principle plugin file (that is important)
  • folders for various file sorts
  • scripts
  • stylesheets
  • embody information to organise the code

Let’s take a look at every of those.

The Primary Customized Plugin File

The primary {custom} plugin file is important. It’s going to at all times be a PHP file, and it’ll at all times include commented-out textual content that tells WordPress about your {custom} plugin. 

Right here’s an instance, from the Akismet plugin:

This tells WordPress what your plugin does, the place to search out out extra about it, and who developed it. It additionally offers details about the model quantity and the textual content area and path for internationalisation, in addition to the license.

WordPress takes this data and makes use of it to populate the plugins display in your web site. Right here’s how Akismet seems to be on that display:

Akismet in the plugins admin screenAkismet in the plugins admin screenAkismet in the plugins admin screen

You may see that the knowledge supplied within the plugin file is used to populate this entry and supply hyperlinks.

Different details about the plugin is contained in the README.txt file, which is used to populate the plugin’s web page within the plugin listing:

Akismet in the plugins directoryAkismet in the plugins directoryAkismet in the plugins directory

The primary plugin file can even include the code that makes the plugin run. Typically that might be all of the PHP for the plugin, however for bigger plugins, there’ll be calls to incorporate information containing additional code. This helps you organise your code and means you don’t have one lengthy, disorganised file that’s troublesome to work with. I’ll present you how you can use embody information later on this information.

Folder Construction

Whereas there are not any arduous and quick guidelines on the way you organise the folders in your {custom} WordPress plugin, it is sensible to undertake the identical construction that different plugin builders use. This may familiarise you with the best way different plugins are constructed and imply that in case you share your code in future, it would make sense to different individuals.

Folders in your plugin may embody:

  • css or types for stylesheets
  • scripts for JavaScript
  • contains for embody information
  • templates for template information that your plugin outputs
  • property for media and different asset information
  • i18n for internationalisation information

You may discover you should use extra folders to construct a WordPress plugin in case your plugin is massive or advanced. For instance, WooCommerce has folders for packages, pattern knowledge, and extra. These in flip embody subfolders for issues like blocks and admin information.

WooCommerce filesWooCommerce filesWooCommerce files

Scripts and Stylesheets

In case your plugin outputs content material that wants styling, both within the front-end or within the admin screens, chances are you’ll want stylesheets. And in case your plugin will use scripts, you’ll want information for these.

It is sensible to maintain these in their very own folder, even in case you solely have one in all every. You’ll must enqueue these scripts and stylesheets utilizing a devoted perform in your essential plugin file. I’ll present you the way to do that after we’re constructing the plugin.

Embrace Recordsdata

In case your plugin wants organisation, you are able to do this by splitting your code into a number of information, known as embody information. You then put these information into their very own folder and name them in your essential plugin file utilizing an embody or require perform.

This manner, you’ll be able to preserve the majority of your code in a well-organised file construction whereas your essential plugin file stays lean and minimal.

In case your plugin isn’t all that massive, you don’t want to make use of embody information: simply add your code to the principle plugin file. However you may discover you should organise this file and reorder capabilities inside it as you add them, to take care of a logical construction.

These are the most typical parts of a plugin. We’ve seen within the WooCommerce instance that there may be many extra. Or in smaller plugins there may be many fewer. However as you develop extra plugins, you’ll end up utilizing these parts increasingly.

Easy methods to Run Your Plugin Code: Choices

Whenever you add code to your plugin, it received’t do something till you activate it not directly. There are a couple of strategies you need to use to activate your code or pull in code from WordPress:

  • capabilities
  • motion and filter hooks
  • lessons

Let’s check out every of those.


Capabilities are the constructing blocks of WordPress code. They’re the simplest method to get began writing your personal plugins and the quickest to code. You’ll discover loads of them in your themes’ information too.

Every perform could have its personal title, adopted by braces and the code inside these braces.

The code inside your plugin received’t run except you name the perform one way or the other. The best (however least versatile) means to do this is by immediately calling the code in your theme or elsewhere in your plugin.

Right here’s an instance perform:

To immediately name that perform in your theme, you’d merely kind tutsplus_myfunction() within the place in your theme template information the place you need it to run. Otherwise you may add it someplace in your plugin… however you’d additionally must activate the code that calls it!

There are a couple of limitations to this:

  • If the perform does one thing that isn’t simply including content material someplace in a theme template file, you’ll be able to’t activate it this manner.
  • If you wish to name the perform in a number of locations, you’ll must name it many times.
  • It may be arduous to maintain observe of all of the locations you’ve manually known as a perform.

It’s significantly better observe to name capabilities by attaching them to a hook.

Motion and Filter Hooks

By attaching your perform to a hook, you run its code every time that hook is fired. There are two varieties of hook: motion hooks and filter hooks.

Motion hooks are empty. When WordPress involves them, it does nothing except a perform has been hooked to that hook.

Filter hooks include code that can run except there’s a perform hooked to that hook. If there’s a perform, it’ll run the code in that perform as an alternative. This implies you’ll be able to add default code to your plugin however override it in one other plugin, or you’ll be able to write a perform that overrides the default code that’s hooked up to a filter hook in WordPress itself.

Hooks are fired in 3 ways:

  • By WordPress itself. The WordPress core code contains lots of of hooks that fireplace at completely different occasions. Which one you hook your perform to will depend upon what your perform does and if you need its code to run. You will discover an inventory of WordPress hooks within the developer handbook.
  • By your theme. Many themes embody motion and filter hooks that you need to use so as to add additional content material in key locations in your web site’s design. And all themes will embody a wp_head and wp_footer hook. Mix these with conditional tags, and you may run particular code on sure varieties of pages in your web site.
  • By your plugin or different plugins. You may add an motion hook to your plugin after which add capabilities in your embody information that connect code to that hook. Otherwise you may write a filter hook after which have a perform that overrides its contents below sure circumstances. Alternatively, in case you’re making a plugin to enrich one other plugin, you’ll be able to hook your capabilities to the prevailing hook within the third-party plugin. I’ve finished this with WooCommerce, for instance, to customize what’s output on product pages.

A few of that is extra superior, however together with your first plugin, you’ll most likely be hooking your capabilities to an motion or filter hook output by WordPress itself, probably an motion hook.


Courses are a means of coding extra advanced options, reminiscent of widgets and customizer parts, that make use of the prevailing WordPress APIs. 

Whenever you write a category in your plugin, you’ll most likely be extending an present class that’s coded into WordPress. This manner, you may make use of the code supplied by the category and tweak it to make it your personal. An instance could be the customizer, the place you may write a category together with a shade picker, making use of the colour picker UI that’s supplied within the present class for the customizer.

Utilizing lessons is extra superior than capabilities, and it’s unlikely you’ll do it in your first plugin. To seek out out extra, see our information to lessons in WordPress.

Should you do write lessons, you’ll nonetheless have to make use of actions or filters to get them to run.

Greatest Practices to Make a WordPress Plugin

Earlier than you begin coding your plugin, it helps to grasp greatest practices for plugins so your code may be prime quality proper from the beginning. 

Listed below are some greatest practices for making a WordPress plugin:

  • Write your code in line with WordPress coding requirements. If you wish to submit your plugin to the plugin listing, you’ll have to do that.
  • Use feedback all through your code so different individuals can work with it—and so that you bear in mind how your code works if you come again to it sooner or later.
  • Identify your capabilities, hooks, and lessons utilizing prefixes so they’re distinctive to your plugin. You don’t need to give a perform the identical title as one other perform in a distinct plugin or in WordPress core.
  • Organise your folders logically, and preserve your code separated so different individuals can perceive it and so you’ll be able to add to it over time with out it turning into a large number.

You may assume that utilizing greatest observe isn’t obligatory as a result of it’s simply you working with the plugin. However your {custom} WordPress plugin may develop over time, you may let different individuals use it, otherwise you may promote it. Otherwise you may come again to it in two years and never be capable of bear in mind how the code is organised! These are necessary issues to remember earlier than you learn to create a WordPress plugin.

Easy methods to Make a WordPress Plugin in 4 Steps

Finally! You have got a grounding in how plugins work, and it’s time to roll your sleeves up and create your first plugin. I’m going to take you thru the method of making a easy plugin that registers a {custom} put up kind.

This can be a quite common use of a plugin, and one thing you may then construct on over time so as to add {custom} template information in your {custom} put up kind or different performance.

I’ll present you the essential code for the plugin and provide you with an introduction to the way you may add to it over time.

1. Create the WordPress Plugin Folder

Even when your plugin is beginning out small with only one file, it’s good observe to provide it its personal folder. Begin by making a folder in your wp-content/plugins listing. Inside that, create a PHP file in your plugin.

Give them each a reputation that is sensible and features a prefix. I’m calling my folder tutsplus-register-post-types and my file tutsplus-register-post-types.php.

Now open your plugin file and add the commented-out data on the high. You may take mine under and edit it to replicate the truth that that is your plugin, not mine.

Now, in case you save your file and go to the Plugins display in your improvement web site, you’ll see the plugin on the display:

New plugin in the plugins admin screenNew plugin in the plugins admin screenNew plugin in the plugins admin screen

You may activate it if you would like, however it received’t do something but since you haven’t added any code to it. Let’s try this.

2. Add Capabilities to Your Customized Plugin

Now it’s time to jot down the primary perform in our plugin. Begin by making your plugin and including the braces which can include the code. Right here’s mine:

This contains all of the labels and arguments in your put up kind and (crucially) the register_post_type() perform which is supplied by WordPress.

I’ve used films as my put up kind right here as I’m creating an imaginary film evaluate web site. You may need to use one thing completely different.

Now, in case you save your file and return to your web site, you’ll see that nothing has modified. That’s since you haven’t activated your code. The strategy we use to activate the perform right here is by hooking it to an motion hook supplied by WordPress, the init hook. Whenever you use a perform supplied by WordPress (reminiscent of register_post_type), you’ll discover that there’s a hook that it is best to use. You will discover particulars within the WordPress handbook entry for registering {custom} put up sorts.

So let’s add the hook. Below your code, and out of doors the braces, add this line:

We use the add_action() perform to hook our code to an motion hook, with two parameters: the title of the motion hook and the title of our perform.

Now attempt saving your information and going again to your web site. You’ll see that the {custom} put up kind has been added to your admin menu (assuming you’ve activated the plugin).

New post type in admin menuNew post type in admin menuNew post type in admin menu


Now let’s add an additional perform, to register a {custom} taxonomy. Beneath the code you’ve written to this point, add this:

Once more, you may need to change the title of your {custom} taxonomy. Right here, I’ve made the taxonomy apply to the put up kind I’ve simply registered (the third parameter of the register_taxonomy perform). Should you gave your put up kind a distinct title, ensure that to edit that bit.

Now save your file and try your admin screens. Whenever you hover over your put up kind within the admin menu, you’ll see the brand new taxonomy.

New taxonomy inn WordPress adminNew taxonomy inn WordPress adminNew taxonomy inn WordPress admin

You now have a working plugin. Nicely finished!

Let’s check out the way you may add to it.

3. Enqueue Stylesheets and Scripts

If you should use {custom} styling or scripts in your plugin, you possibly can add them proper into your plugin file—however that’s not greatest observe. As an alternative, it is best to create stylesheets and scripts as separate information in your plugin folder and enqueue these, utilizing a perform supplied by WordPress.

Let’s think about you need to add styling in your {custom} put up kind. You may add this to your theme, however you may need to add some particular styling to the plugin to make the put up kind stand out from different put up sorts in any theme.

To do that, you create a brand new folder inside your plugin folder known as css (or types, it’s as much as you). Inside that folder, create a stylesheet known as fashion.css, otherwise you can provide it a extra particular title for readability. I’m going to name mine films.css.

You then must enqueue that file in your plugin in order that it may be utilized by WordPress. Add this to your essential plugin file, above the capabilities you have already got. I like so as to add enqueuing and contains first in my plugin file so I can see what different information are being activated.

Should you save your file, you received’t see any distinction in your admin screens—however in case you’ve added posts of the {custom} put up kind and your stylesheet contains styling for them, you’ll now see that within the front-end of your web site.

Observe that the hook used for enqueuing each stylesheets and scripts is similar: they each use wp_enqueue_scripts. There isn’t a separate hook for types.

Enqueuing scripts works in a really comparable means. Observe these steps:

  • Add a scripts or js folder to your plugin folder.
  • Save your script information in that folder.
  • Enqueue the script in the identical means because the stylesheet above, changing the wp_enqueue_style() perform with wp_enqueue_script().

4. Utilizing Embrace Recordsdata

Another choice as you develop your plugin is to create additional PHP information, referred to as embody information. In case you have quite a lot of these, you may create a number of folders for several types of embody file, otherwise you may simply create one folder known as contains.

There are a couple of capabilities you need to use to incorporate information, which you’ll discover in our complete information to together with and requiring information.

For instance, in our {custom} put up kind plugin, we would create some code to differ the best way the content material of the web page is output, utilizing the the_content filter hook to amend the code being run every time the content material is output on a product web page.

As an alternative of including this code to the principle plugin file, you possibly can add it to a separate file known as movie-content.php after which write the code in that file for the best way the content material is output for films.

To incorporate this file in your plugin, you add a folder known as contains to your plugin, after which inside that folder you add the content-movie.php file.

To incorporate that file in your plugin, you add this code at the start for the principle plugin file:

You don’t must hook this to an motion or filter hook—simply use the include_once() perform in your plugin file. That may then name the code from the embody file as if it was in your essential plugin file at that time.

Easy methods to Lengthen or Edit an Current Customized WordPress Plugin

Typically you may discover a plugin within the plugin listing or from a plugin vendor that does most of what you want a plugin to do, however not fairly all. Otherwise you is perhaps operating a plugin and need to make some tweaks and customisations.

The truth that WordPress is open supply makes this doable. You may take one other plugin’s code and prolong or edit it to make it work the best way you need it to.

There are two methods to do that:

  • Take an present plugin and fork it—i.e. edit it so it really works in a different way, is extra dependable, or has additional options.
  • Write your personal plugin that extends the unique plugin.

Taking an present plugin and modifying it’s pretty simple: you make your personal copy on a improvement web site (by no means reside!) and make edits to it as wanted. Be sure to use model management to trace your adjustments in case one thing goes improper.

Extending a plugin by writing your personal plugin is barely extra sophisticated, and received’t work with each plugin, however is a extra sturdy means of doing issues for my part.

Lots of the hottest plugins will make in depth use of hooks and lessons of their code. You may hook into motion and filter hooks and prolong lessons to jot down your personal code, which makes use of the prevailing plugin’s code as a base however then provides to or edits it.

For instance, WooCommerce has so many capabilities, hooks, and lessons that it has its personal API and developer documentation. Every a part of the WooCommerce system is powered by a number of of those capabilities, hooks, or lessons. To make adjustments, you should establish which code is driving the a part of the system you need to change, after which write your personal plugin which both attaches to the identical hook(s) or extends the lessons.

You’ll discover you’ll be able to create vital customisations to a plugin like WooCommerce on this means: I as soon as used it to energy a listings web site that didn’t even embody a checkout. I used hooks to take away all the weather I didn’t need and add new ones.

For some examples of how one can hook into WooCommerce and prolong or edit it with your personal plugin, see our information to including product descriptions to archive pages and to including a product-based weblog to your retailer. These examples simply scratch the floor of what you’ll be able to obtain, however they’re going to provide you with an concept of how you can begin.

Taking It Additional: Plugin Options

On this information, I’ve proven you how plugins are coded and how you can get began constructing a easy plugin.

When you’ve acquired the grasp of plugin improvement, you’ll be able to construct extra advanced and full-featured plugins to hold out extra advanced duties in your web site.

Let’s check out some examples.

Create Your WordPress Plugin Tutorials

Widget plugins contain working with lessons however are a great introduction to the subject. Our information to making a widget plugin will enable you do it.

On our weblog, we’ve recognized the greatest widget plugins for 2021, the greatest Fb widgets, and the greatest Twitter widgets.

Shortcodes to Create a Plugin for WordPress

Making a shortcode is a superb place to begin creating plugins as they’re comparatively easy and really helpful. Learn how to create them with our information to coding shortcodes in WordPress.

Social Media Plugins

Social media plugins are extremely fashionable as they allow you to show your Fb, Twitter, or Instagram feed in your web site and let your guests share your content material by way of their very own social media accounts.

Study in regards to the greatest social media plugins and how you can create a web based group in your WordPress web site.

Gallery and Media Plugins

If you wish to optimise the media in your web site and show galleries or video feeds, a plugin will make every thing look extra skilled. Learn how to search out the greatest gallery plugins for pictures or video and how you can code your personal gallery plugin

Kind Plugins

Including kinds to your web site lets your guests get in contact and helps construct a relationship. CodeCanyon has loads of premium type plugins that can make it straightforward in your guests to contact you. Study how you can create a type with the bestselling QuForm plugin.

FAQs on Easy methods to Create a WordPress Plugin

Listed below are the solutions to a number of the most continuously requested questions on WordPress plugins.

Why can’t I simply add the code I must my theme capabilities file?

It’s tempting to easily carry on including code to the capabilities.php file, and there may be some code that ought to be there.

But when your code is said to performance in your web site, quite than the design or the output of content material, then it is best to code it right into a plugin. Which means in case you swap themes sooner or later, you continue to have that performance. And you need to use the plugin on one other web site operating a distinct theme.

I’ve added code to my plugin. Why is nothing taking place?

That is most likely since you haven’t hooked your code to an motion or filter hook. Till you try this, nothing will occur.

After I edit my plugin and test my web site, I get a white display. Assist!

You’ve most likely added some code that’s acquired an error in it someplace. PHP is an unforgiving language, and this is perhaps as minor as a semicolon within the improper place.

Attempt turning on WP_DEBUG in your wp-config.php file, and also you’ll see a message telling you the place the error is. Then you’ll be able to repair it.

After I activate my plugin, I get an error message telling me too many headers have been output. What does this imply?

All this usually means is that there are too many empty traces in your plugin file. Return and test there are not any empty traces at the start of the file.

If that doesn’t repair it, attempt turning on WP_DEBUG.

The place can I discover out extra about creating plugins?

We now have a lot of tutorials and programs serving to you to construct all types of plugins right here on Envato Tuts+. See if yow will discover something that conjures up you.

tutsplus plugins coursestutsplus plugins coursestutsplus plugins courses

The place can I obtain plugins for my web site?

You may select from hundreds of free plugins within the WordPress plugin listing. You can even purchase third-party plugins from CodeCanyon. At all times purchase plugins from a good provider so that you may be positive they comply with WordPress coding requirements and don’t include malicious code.


Plugins will flip your web site from a easy weblog into a robust web site that features superior options and is safe and sturdy. Attempt including plugins to your web site right now and coding your personal to see how you need to use plugins to enhance your WordPress web site.

Listed below are a couple of of the best-selling and up-and-coming WordPress themes and plugins accessible for 2020.


Most Popular

Recent Comments