首页 > IT > 装了Executable PHP widget

装了Executable PHP widget

2007年8月4日

刚刚新装了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'];
?>
<?php echo $before_widget; ?>
<?php $title ? print($before_title . $title . $after_title) : null; ?>
<div class="execphpwidget"><?php eval('?>'.$text); ?></div>
<?php echo $after_widget; ?>
<?php
}

function widget_execphp_control($number) {
$options = $newoptions = get_option('widget_execphp');
if ( $_POST["execphp-submit-$number"] ) {
$newoptions[$number]['title'] = strip_tags(stripslashes($_POST["execphp-title-$number"]));
$newoptions[$number]['text'] = stripslashes($_POST["execphp-text-$number"]);
if ( !current_user_can('unfiltered_html') )
$newoptions[$number]['text'] = stripslashes(wp_filter_post_kses($newoptions[$number]['text']));
}
if ( $options != $newoptions ) {
$options = $newoptions;
update_option('widget_execphp', $options);
}
$title = htmlspecialchars($options[$number]['title'], ENT_QUOTES);
$text = htmlspecialchars($options[$number]['text'], ENT_QUOTES);
?>
<input style="width: 450px;" id="execphp-title-<?php echo "$number"; ?>" name="execphp-title-<?php echo "$number"; ?>" type="text" value="<?php echo $title; ?>" />
<p>PHP Code (MUST be enclosed in <?php and ?> tags!):</p>
<textarea style="width: 450px; height: 230px;" id="execphp-text-<?php echo "$number"; ?>" name="execphp-text-<?php echo "$number"; ?>"><?php echo $text; ?></textarea>
<input type="hidden" id="execphp-submit-<?php echo "$number"; ?>" name="execphp-submit-<?php echo "$number"; ?>" value="1" />
<?php
}

function widget_execphp_setup() {
$options = $newoptions = get_option('widget_execphp');
if ( isset($_POST['execphp-number-submit']) ) {
$number = (int) $_POST['execphp-number'];
if ( $number > 9 ) $number = 9;
if ( $number < 1 ) $number = 1;
$newoptions['number'] = $number;
}
if ( $options != $newoptions ) {
$options = $newoptions;
update_option('widget_execphp', $options);
widget_execphp_register($options['number']);
}
}

function widget_execphp_page() {
$options = $newoptions = get_option('widget_execphp');
?>
<div class="wrap">
<form method="POST">
<h2>PHP Code Widgets</h2>
<p style="line-height: 30px;"><?php _e('How many PHP Code widgets would you like?'); ?>
<select id="execphp-number" name="execphp-number" value="<?php echo $options['number']; ?>">
<?php for ( $i = 1; $i < 10; ++$i ) echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : ").">$i</option>"; ?>
</select>
<span class="submit"><input type="submit" name="execphp-number-submit" id="execphp-number-submit" value="<?php _e('Save'); ?>" /></span></p>
</form>
</div>
<?php
}

function widget_execphp_register() {
$options = get_option('widget_execphp');
$number = $options['number'];
if ( $number < 1 ) $number = 1;
if ( $number > 9 ) $number = 9;
for ($i = 1; $i <= 9; $i++) {
$name = array('PHP Code %s', null, $i);
register_sidebar_widget($name, $i <= $number ? 'widget_execphp' : /* unregister */ ", $i);
register_widget_control($name, $i <= $number ? 'widget_execphp_control' : /* unregister */ ", 460, 350, $i);
}
add_action('sidebar_admin_setup', 'widget_execphp_setup');
add_action('sidebar_admin_page', 'widget_execphp_page');
}
// Delay plugin execution to ensure Dynamic Sidebar has a chance to load first
widget_execphp_register();
}

// Tell Dynamic Sidebar about our new widget and its control
add_action('plugins_loaded', 'widget_execphp_init');

?>


相关文章:

IT

  1. 目前还没有任何评论.
  1. 目前还没有任何 trackbacks 和 pingbacks.