How do plugins in Wordpress work?

Posted on 2015-02-03
Medium Priority
Last Modified: 2016-02-25
This question is not: how do I make a plugin.
This question is not: I need a plugin that does X

This question is literally: how do they work?

I have a project that is coming up that would benefit greatly from modular deveopment - we can create the base system, and then develop the components of the system modularly (as plugins).

I've reviewed the source for Wordpress, and it seems to escape me on how they actually work. Are they a call back?

How would I go about implementing a simplified version of the Wordpress plugin system in custom software?

Question by:DrDamnit
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 51

Expert Comment

by:Steve Bink
ID: 40587782
First, two caveats:


This is a very in-depth topic not particularly suited for this forum.  We can give general education and guidance, but your best source of knowledge is going to be a working code base, e.g., Wordpress.  


While I'm not intimately familiar with the plugin architecture for Wordpress, I am very familiar with Joomla's system.
In Joomla, the plugin architecture is split into four areas: components, modules, plugins, API.  The first three are collectively known as extensions, while the fourth is the actual engine behind making extensions of any type work.  

To walk through the basics, let's start with a request coming into a Joomla site.  Joomla receives the request and bootstraps itself.  It then examines the request for clues as to what it is supposed to deliver in response.  The first question is needs to answer is which component needs to do the processing.  Joomla's database maintains an inventory of installed components, all of which are required to have a basic, minimum structure in order for Joomla to find their physical location and launch into their code.  For example, they are required to have an entrance file named for their extension.  Once inside the component, it can do literally anything - the entirety of PHP is available to it with the addition of Joomla's ready-made environment.

For the sake of brevity, let's assume the request will generate an article.  The article component of Joomla loads the template, content, etc.  Part of the template specifies positions for tertiary content, or modules.  Again, each module requires registration with Joomla so the CMS knows where to find its code and how to run it.  Each instance of an installed module extension will be assigned a position (and possibly a filter of menu items under which it will appear).  As Joomla begins rendering the template and it comes across a position definition, it will query the database for any modules assigned to that position.  For each module it discovers, it calls the module's entrance function and waits for it to return.  As with components, the module can do virtually anything.  Modules have a few more restrictions since they are designed to return a section of content, but the possibilities are very open.

Through all of this process, Joomla is firing hooks at specific points, known as "Events".  You might want to take a look at the Joomla list of events to see what hooks are possible.  The event fires by calling a specific function inside Joomla's plugin architecture.  That function cycles through an internal list of plugins which have registered a hook to that specific event.  For example, one of the built-in plugins with Joomla is used to mask email addresses.  I *believe* this fires with the onContentPrepare event.  When it fires, the plugin's main function will be called and will receive the content, as well as some other parameters.  It is free to manipulate the article and the parameters as it wishes, then exits.  In the case of the address scrubber, it does a regex search for anything looking like an email address and obfuscates it.

So, boiled down, the basics are that Joomla provides a platform through which extensions can register themselves.  As opportunities arise for extensions to do work, Joomla looks through those registrations for anything applying to the specific context at the time of the call.  In turn, components, modules, and plugins are all responsible for adhering to the protocol Joomla has outlined.

Finally, there's the API, which is really becoming the core of the Joomla system moving forward.  In newer Joomla versions, the CMS is actually an extension application which utilizes the Joomla API to do its work.  There is an important distinction here - using the Joomla API frees an application from the restrictions inherent with working inside the Joomla CMS.  In theory, you could write a brand new CMS on top of the API without using the Joomla CMS at all.

This is a very, very high-level overview of how the Joomla plugin architecture works.  For any real knowledge, you're going to have to start exploring the code.  Luckily, Joomla and Wordpress are both open source, so that option is there for you.  I learned all I did about Joomla by tracing a single request from bootstrap to response.  Any CMS is very complicated application, with thousands of moving parts.  Expect to invest a significant amount of time learning even a portion of the mechanics behind any of them.

Next up, magnets...  How do THEY work?

Expert Comment

by:Brandon Lyon
ID: 40589117
Are we talking about a frontend plugin or a backend plugin? Either way you can think of them as includes that will add, replace, or modify functionality wherever you call the plugin. You can start the plugin via a function or include in the relevant templates if it's a widget. If it's not a widget then just by defining it as a plugin Wordpress knows it can be activated and will run when plugins run. You may ask it to connect to the database and fetch information. You might create files. You might insert something into a loop. You might entirely replace one part of their system with a different one instead. It really depends on what you're trying to do exactly.
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40595026

Are they a call back?

Pretty much, yes.  There is some variation depending on what the plugin actually does but for the most part this is how they work:

Assuming you have a bunch of plugins installed:
1) Plugins defined as "must use" in wp-settings.php get loaded first
2) If running in network mode, plugins that are defined across the network get loaded next
3) The rest get loaded in order per the active_plugins entry in wp_options

ref: http://codex.wordpress.org/Action_Reference#Actions_Run_During_a_Typical_Request

4) Now, stuff is loaded and waiting for input.  WordPress now runs the init action which can usually see a logged in user and any GET or POST stuff and act on it.  Your plugin(s) are simply a bunch of callbacks that use the WordPress hook and filter system to modify the core behavior at this point in time using add_action().  If you are trying to do something that does not have a hook, you either need to define a new one in the plugin or work around it using something else (system command, other PHP functionality, external call) to achieve the same result.

ref: http://codex.wordpress.org/Plugin_API/Action_Reference/init

5) After everything finishes process, headers are sent and the browser renders.

Is this sort of what you're after?
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

LVL 32

Author Comment

ID: 40595595

This is sort of what I am after. What I really want is the programmatic mechanism that makes plugins... plugins.

What I think happens is Wordpress starts up, and prior to "the loop":
1. The plugins directory is scanned for files that could be included.
2. The database is checked to see which ones are activated.
3. If it finds a plugin that is activated it loads that file with an include()

Here's where I am either getting lost, or making it too hard:

I know the plugins are kept in an array because I have written plugins that needed to be loaded in a specific order, and you can't load plugins in any order other than how they exist in the file system. So, my solution at the time was to rename the plugins so that they would load in the correct order.

So, my question is more of this: if they are included from the file system, how do they get into an array? (In C++, we can define a pointer to a function, which would fulfill this purpose, but I don't see where WP is doing that).

Secondly, once they are in the array, when are they called?
LVL 32

Author Comment

ID: 40595596
Perhaps it is also useful to say: "I want to clone the WP plugin system", which this is effectively true. I want to borrow from how it works to create a system nearly identical to the plugins system in WP, but for an entirely different purpose.
LVL 51

Expert Comment

by:Steve Bink
ID: 40596034
With open source systems such as Wordpress and Joomla, it would be easy enough to rip out the important parts you want and adapt them to your own environment.  However, this all hinges on your understanding of how these parts work.  

In your previous comment, you mentioned "what I think happens", and talked about inferences based on observed behavior.  While that's fine for an overview of how the system works in that specific implementation, you'll need to get neck-deep in the code to really understand what it does and why.  My recommendation stands: clone the git repo for Wordpress and begin tracking through the code.

Expert Comment

by:Brandon Lyon
ID: 40596405
Believe it or not, there is a plugin to change the order plugins load...

I also found a very old post ( ~ 4 years old ) describing more about how the plugin load order works. https://wordpress.org/support/topic/how-to-change-plugins-load-order. The array you're speaking of is probably the list in the database of active plugins. It sounds like it's possible to change the order by using a filter to set load priority.
LVL 70

Accepted Solution

Jason C. Levine earned 2000 total points
ID: 40608308

Here's a good synopsis of how WordPress loads things:


Basically one of my posts above, expanded to great detail.

Also, you may want to look at the BackPress project: http://backpress.org/features/ as it's essentially WordPress stripped down to the core libraries with the intent to be able to use for any web application.
LVL 32

Author Closing Comment

ID: 40692795
Compare and contrast between wordpress core and back press seems to give me what I am looking for.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question