4

Wordpress uses the Observer Pattern for its plugin system and operates on the premise of checking if a plugin is activated, scanning the plugins directory and storing everything in an array in the plugins class.

Once it gets the activated plugins, it then proceeds to include the plugin script files, however I am curious as to how Wordpress un-includes plugins when they are deactivated in the admin? If Wordpress is merely including the file via a standard include_once or require_once, how does Wordpress deactivate a plugin and therefore un-include it?

It's from my understanding that you cannot include and then un-include a file in PHP (not that I know of). Can anyone point to me specific files where the plugins are included, and the appropriate functions and files where they are deactivated?

If Wordpress is storing activated plugins in an array of activated plugins, then including them; how does Wordpress know not to let a function inside of a deactivated plugin be called if it's already included?

1 Answer 1

8

The active plugins are stored in the 'active_plugins' option, like this:

array(
  'akismet/akismet.php',
  'hello-dolly.php',
);

On each page load, WP just loops through that array and includes those files. It's up to each plugin to include any additional files that it may have.

When a plugin is deactivated, it's just removed from that array. On the next page load, it won't be included anymore.

No "un-including" required.

8
  • But for a function inside of a script to be called, it needs to be included right? You can't call a function from within a script without first somehow loading it can you? So if you have a show_message function inside of a plugin and then deactivate it, does Wordpress first check if that function is inside of a deactivated plugin and not call it? Mar 21, 2011 at 0:27
  • In wp-settings the following is being run, the inclusion of all activate plugins: foreach ( wp_get_active_and_valid_plugins() as $plugin ) include_once( $plugin ); unset( $plugin ); - The question is how does Wordpress stop deactivated plugin functions being run? The code base is a mess, it's hard to find out what's happening where. Mar 21, 2011 at 0:38
  • 4
    The above would only happen when the "deactivate plugin" script execution is run, and yes, the plugin will still be able to run anything for that script execution, and it will be sent the deactivate hooks also. It is not truly deactivated until the next page load. From what I understand, anyway.
    – Joe Hoyle
    Mar 21, 2011 at 0:57
  • Joe Hoyle is right. The key is that it doesn't happen in the same page load. Updated answer.
    – scribu
    Mar 21, 2011 at 1:26
  • Ah, I see now. So Wordpress obviously uses an include_once or require_once which will load that plugin file for each specific page load and not all of the time? It just keeps checking the array for active plugins and including them, hardly seems efficient, but I guess it works. Mar 21, 2011 at 1:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.