Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer
    
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? –  DigitalSea Mar 21 '11 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. –  DigitalSea Mar 21 '11 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 '11 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 '11 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. –  DigitalSea Mar 21 '11 at 1:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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