Solved

How do plugins in Wordpress work?

Posted on 2015-02-03
9
178 Views
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?

Ref:
https://github.com/WordPress/WordPress/blob/master/wp-includes/plugin.php
0
Comment
Question by:DrDamnit
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 50

Expert Comment

by:Steve Bink
ID: 40587782
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?
0
 
LVL 8

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.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40595026
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?
0
 
LVL 32

Author Comment

by:DrDamnit
ID: 40595595
@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?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 32

Author Comment

by:DrDamnit
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.
0
 
LVL 50

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.
0
 
LVL 8

Expert Comment

by:Brandon Lyon
ID: 40596405
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.
0
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 40608308
DrD,

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

http://carlalexander.ca/anatomy-wordpress-loading/

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.
0
 
LVL 32

Author Closing Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses four methods for overlaying images in a container on a web page
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now