Apr 02, 2007
(一)中讲到了wordpress是如何读取系统中所有插件的。
现在我们来看看她是如何激活及停用插件的。
激活及停用插件
wordpress的当前使用的插件列表存放在数据库中,wp_options表,字段option_name值为active_plugins的列存放的就是当前系统中使用的插件。
a:2:{i:0;s:9:"hello.php";i:1;s:16:"wp-db-backup.php";}
就是当前系统中使用的两个插件Hello Dolly、WordPress Database Backup。
这个字段原有类型是array,经过serialize后存放在数据库中,读出时unserialize。
取得当前系统中使用的插件 functions.php | 273行 | function get_settings($setting) {}
- // 这个方法是通用方法,不光是用来读取当前使用插件
- // 当 $setting = active_plugins 时读取当前使用的插件
- $row = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting' LIMIT 1");
- // ......
- if( is_object( $row) ) {
- $value = $row->option_value;
- // 存入缓存中,下次直接从缓存中取值不用再查询数据库
- wp_cache_set($setting, $value, 'options');
- }
- // 对于active_plugins,只是将值unserialize返回
- return apply_filters( 'option_' . $setting, maybe_unserialize($value) );
- // unserialize前的值
- // a:2:{i:0;s:9:"hello.php";i:1;s:16:"wp-db-backup.php";}
- // unserialize后的值
- /*
- Array
- (
- [0] => hello.php
- [1] => wp-db-backup.php
- )
- */
激活插件
通过链接plugins.php?action=activate&plugin=hello.php
wp-admin/plugins.php | 5行左右 |
- // 取得当前系统中使用的插件
- $current = get_settings('active_plugins');
- // 待激活插件不在已激活插件列表中
- if (!in_array($_GET['plugin'], $current)) {
- // 将新插件添加到已激活插件列表中
- $current[] = trim( $_GET['plugin'] );
- sort($current);
- // 将当前正在使用的插件存放到数据库中
- // 对于active_plugins,只是将当前使用的插件列表serialize后存放进数据库
- update_option('active_plugins', $current);
- // ......
- }
停用插件
通过链接plugins.php?action=deactivate&plugin=hello.php
wp-admin/plugins.php | 16行左右 |
- // 取得当前系统中使用的插件
- $current = get_settings('active_plugins');
- // 先搜索待停用插件在使用插件中的位置,再将插件列表这部分内容去掉
- array_splice($current, array_search( $_GET['plugin'], $current), 1 );
- // 将删除待停用过后的插件列表存放到数据库中
- // 对于active_plugins,只是将当前使用的插件列表serialize后存放进数据库
- update_option('active_plugins', $current);
- // ......



本文相关评论|Comments