关于免插件的一点小想法(效率问题)

关于WP的插件机制,网上已经有相当多的文章了,不过看来看去还就那么几篇,好多COPY的,呃。还是自己看下WP代码。
在wp-settings.php文件中可看到如下代码 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//从数据库获得当前处于激活状态的插件数组
$current_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
//遍历插件数组
if ( is_array($current_plugins) && !defined('WP_INSTALLING') ) {
    foreach ( $current_plugins as $plugin ) {
        // check the $plugin filename
        // Validate plugin filename
        if ( validate_file($plugin) // $plugin must validate as file
            || '.php' != substr($plugin, -4) // $plugin must end with '.php'
            || !file_exists(WP_PLUGIN_DIR . '/' . $plugin)  // $plugin must exist
            )
            continue;
               // 如果插件名合法并且文件存在,就把插件文件include进来。
        include_once(WP_PLUGIN_DIR . '/' . $plugin);
    }
    unset($plugin);
}

再往后面看可以看到如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
 * Web Path to the current active template directory
 * @since 1.5.0
 */

define('TEMPLATEPATH', get_template_directory());

/**
 * Web Path to the current active template stylesheet directory
 * @since 2.1.0
 */

define('STYLESHEETPATH', get_stylesheet_directory());

/*此处省略无关代码若干*/
// Load functions for active theme.
if ( TEMPLATEPATH !== STYLESHEETPATH && file_exists(STYLESHEETPATH . '/functions.php') )
    include(STYLESHEETPATH . '/functions.php');
if ( file_exists(TEMPLATEPATH . '/functions.php') )
    include(TEMPLATEPATH . '/functions.php');

由此可见,不管你是在插件中用add_filter 、add_action还是 在主题的functions.php中通过添加一两个或者更多的函数然后再add_filter ,其结果都是一样,全部都要被include进来的。因此,不能盲目地追求“免插件”。不能一楖地认为没装插件就一定比装了插件效率高速度快,其实不然。得看什么情况。 当然,一般情况下,插件都有后台,因此插件会稍微多一些东西。所以,不用插件应该会稍微快一点,但是由此而付出的代价是用起来没有那么方便了,插件的好处是可配置性强。
当然,用模板的情况就不同了。只有当需要用到这个模板时,WP才会加载它。
在wp-includes\theme.php(842)可看到:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
 * Retrieve path of page template in current or parent template.
 *
 * Will first look for the specifically assigned page template
 * The will search for 'page-{slug}.php' followed by 'page-id.php'
 * and finally 'page.php'
 *
 * @since 1.5.0
 *
 * @return string
 */

function get_page_template() {
    global $wp_query;

    $id = (int) $wp_query->post->ID;
    $template = get_post_meta($id, '_wp_page_template', true);
    $pagename = get_query_var('pagename');

    if ( 'default' == $template )
        $template = '';

    $templates = array();
    if ( !empty($template) && !validate_file($template) )
        $templates[] = $template;
    if ( $pagename )
        $templates[] = "page-$pagename.php";
    if ( $id )
        $templates[] = "page-$id.php";
    $templates[] = "page.php";

    return apply_filters('page_template', locate_template($templates));
}

这是WP获得当前模板目录中特定页面模板的函数。
在wp-includes\template-loader.php(36)我们还可以看到如下语句:

1
2
3
4
if ( is_page() && $template = get_page_template() ) {
        include($template);
        return;
    }

由此可见,只有是“页面”,并且页面存在自己的特定模板时,WP才会加载它(把它include进来).
而 template-loader.php是在 wp-blog-header.php中加载的,整个wp-blog-header.php文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 <?php
/**
 * Loads the WordPress environment and template.
 *
 * @package WordPress
 */

if ( !isset($wp_did_header) ) {

    $wp_did_header = true;

    require_once( dirname(__FILE__) . '/wp-load.php' );

    wp();

    require_once( ABSPATH . WPINC . '/template-loader.php' );

}
?>

由此可见,页面模板文件确实是按需加载的。因此,用页面模板来实现某些要用插件实现的功能,确实是比较高效的。当然,可配置性和易用性可能没有插件那么完美。

喜欢这篇文章吗?

请订阅本站 RSS feed填写您的邮件地址,订阅我们的精彩内容:

相关日志

回复 (6)

  1. Demon  / 回复

    其实。使用免插件是想方便点或者说是让你来折腾一下。 :wink:

  2. moper  / 回复

    :oops: 这个写的不错类~

  3. 求索阁  / 回复

    钻研的好深入哦···

  4. 万戈  / 回复

    晕,这么巧,我今天晚上刚想写这个话题,被你抢先了 :cry:

发表评论 修改评论取消编辑

允许使用的标签 - 您可以在评论中使用如下的 HTML 标签以及属性。

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 :wink:  :-|  :-x  :twisted:  :)  8-O  :(  :roll:  :-P  :oops:  :-o  :mrgreen:  :lol:  :idea:  :-D  :evil:  :cry:  8)  :arrow:  :-?  :?:  :!:

引用通告 (1)

  1. 5:02 下午, 2011年10月2日Favicon of ihacklog.comWordPress优化技巧
开灯