Link to home
Start Free TrialLog in
Avatar of DrDamnit
DrDamnitFlag for United States of America

asked on

How do plugins in Wordpress work?

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?

Ref:
https://github.com/WordPress/WordPress/blob/master/wp-includes/plugin.php
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

First, two caveats:

1.

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.  

2.

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?
Avatar of Brandon Lyon
Brandon Lyon

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.
DrD,

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?
Avatar of DrDamnit

ASKER

@everyone:

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?
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.
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.
Believe it or not, there is a plugin to change the order plugins load...
https://wordpress.org/plugins/plugin-organizer/

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.
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Compare and contrast between wordpress core and back press seems to give me what I am looking for.