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?
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.