WordPress.org

Ready to get started?Download WordPress

Forums

How does the plugin detection and activation code work? (4 posts)

  1. Olmen
    Member
    Posted 7 years ago #

    Hello fellow coders!

    I've always been using WordPress for almost a year now I've always been very impressed with the technology behind it. Most impressive imho is the plugin system.

    I'm myself a hobby developer and I'm interested in experimenting with a similar modular /plugin based design. It's quite easy to make with a database storing information about the modules but then you have to "install" the modules manually to work. What I especially like in wordpress is that it detects the new plugins just by browsing through the plugin folder! That's some neat stuff.

    Unfortunately I don't have a clue how that's done. I've been trying to look at wordpress' source to figure it out but to no good.

    Could someone who actually knows anything about this give me a little heads up? Like how does wordpress read through the files. And how does it read the header's contents?

    Thanks in advance :P

  2. whooami
    Member
    Posted 7 years ago #

    the simple answer to your question is found by just looking at the function, get_plugins:

    function get_plugins() {
    	global $wp_plugins;
    if (isset ($wp_plugins)) {
    		return $wp_plugins;
    	}
    
    	$wp_plugins = array ();
    	$plugin_loc = 'wp-content/plugins';
    	$plugin_root = ABSPATH.$plugin_loc;
    
    	// Files in wp-content/plugins directory
    	$plugins_dir = @ dir($plugin_root);if ($plugins_dir) {
    while (($file = $plugins_dir->read()) !== false) {
    if (preg_match('|^\.+$|', $file))
    continue;
    if (is_dir($plugin_root.'/'.$file)) {
    $plugins_subdir = @ dir($plugin_root.'/'.$file);
    if ($plugins_subdir) {
    while (($subfile = $plugins_subdir->read()) !== false) {
    if (preg_match('|^\.+$|', $subfile))
    continue;
    if (preg_match('|\.php$|', $subfile))
    $plugin_files[] = "$file/$subfile";
    	}
    }
    } else {
    if (preg_match('|\.php$|', $file))
    $plugin_files[] = $file;
    			}
    		}
    	}
    if ( !$plugins_dir || !$plugin_files )
    		return $wp_plugins;
    foreach ( $plugin_files as $plugin_file ) {
    if ( !is_readable("$plugin_root/$plugin_file"))
    continue;
    $plugin_data = get_plugin_data("$plugin_root/$plugin_file");
    
    if ( empty ($plugin_data['Name']) )
    			continue;
    $wp_plugins[plugin_basename($plugin_file)] = $plugin_data;
    	}
    uasort($wp_plugins, create_function('$a, $b', 'return strnatcasecmp($a["Name"], $b["Name"]);'));
    return $wp_plugins;
    }

    thats it, thats how it finds 'em.

  3. whooami
    Member
    Posted 7 years ago #

    the stuff at the top of a plugin is just parsed inside another function:

    function get_plugin_data($plugin_file) {
    $plugin_data = implode('', file($plugin_file));
    preg_match("|Plugin Name:(.*)|i", $plugin_data, $plugin_name);
    preg_match("|Plugin URI:(.*)|i", $plugin_data, $plugin_uri);
    preg_match("|Description:(.*)|i", $plugin_data, $description);
    preg_match("|Author:(.*)|i", $plugin_data, $author_name);
    preg_match("|Author URI:(.*)|i", $plugin_data, $author_uri);
    if (preg_match("|Version:(.*)|i", $plugin_data, $version))
    $version = trim($version[1]);
    else
    $version = '';
    $description = wptexturize(trim($description[1]));
    ....
  4. Olmen
    Member
    Posted 7 years ago #

    Very nice! Thanks a lot! Are these among the admin-functions?

Topic Closed

This topic has been closed to new replies.

About this Topic