刚刚新装了Executable PHP widget这个Wordpress插件。
在访问统计还靠Popularity Contest一文中我指出,“ Wordpress利用Theme和Plug来扩展功能无疑是一个相当优秀的架构,前者主管界面,后者主管功能,理应井水不犯河水。这样才可以确保更换plug无需修改theme,更换theme也无需考虑plug问题。但是,大量需要修改theme代码的plug却打破了这条横沟,把问题复杂化了——尤其是在升级或者更换Theme时,把需要添加的代码放到合适的地方,本身就是一个巨大的工程——更大的问题就在于,这种手工修改代码的工序,原本是可以避免的。”
正是基于这种个人偏好,所以我格外喜欢Executable PHP widget,它允许你在Theme的Widget中添加一个文本框,在里面书写PHP代码。利用这个Widget,很多原本需要针对Theme的代码修改就可以得以避免了。
比如说同样是今天新装的Simple Download counter插件,它可以统计制定文件的下载次数。我已经把我制作的PDF版理性谈风月合集和爱情资本论合集全部交由其监控。这个插件允许调用PHP代码输出指定文件的下载次数,有了Executable PHP widget,我就可以直接在模板中添加一个对应的Widget,把需要的代码写入即可。你现在在首页左侧看到的Download部分,就是它的杰作。
个人觉得,对于许多不懂得修改Theme的Wordpress用户而言,这是一个相当有帮助的Plug。
很可惜,作者的主页被屏蔽了,要下载到这个插件就必须使用代理服务器。考虑到这个Plug只有一个文件,所以笔者这里索性提供源代码,需要的读者直接下载另存为一个名为execphp.php的文件,并上传到plug目录即可。
`
`
<?php
/*
Plugin Name: Executable PHP widget
Description: Like the Text widget, but it will take PHP code as well. Up to 9 instances of this widget may exist. Heavily derived from the Text widget code included with the widget plugin by Automattic, Inc.
Author: Otto
Version: 1.0
Author URI: http://ottodestruct.com
*/function widget_execphp_init(){
// Check for the required API functions
if ( !function_exists(‘register_sidebar_widget’) || !function_exists(‘register_widget_control’) )
return;
function widget_execphp($args, $number = 1) {
extract($args);
$options = get_option(‘widget_execphp’);
$title = $options[$number][’title’];
$text = $options[$number][’text’];
?>
<input style=“width: 450px;” id=“execphp-title-” name=“execphp-title-” type=“text” value="" />
<input type=“hidden” id=“execphp-submit-” name=“execphp-submit-” value=“1” />