打算将GOOGLE的QR码API直接引用系边栏果度,求其啉咗下有三种方案:
1,直接系主题的边栏文件度加,呢个是最简单的,不过静系可以放系最头或者最尾。
2,系现有的文本域边栏度加,不过要加执行PHP代码,同样是简单,不过性能肯定上差咗D。
3,增加一个新的边栏小工具,叫“可执行PHP代码的文本域”,呢种方式自定义性最高,不过无论性能同代码上都唔系最好的。
首先来睇下要用的代码,就唔解释了,用第一种方案的话没咩要讲的。
1 2 3 4 5 6 7 |
<img src="http://chart.googleapis.com/chart?cht=qr&chs=200x200&choe=UTF-8&chld=L|4&chl=<?php if(is_singular()) { the_permalink(); } else { echo get_bloginfo('url').$_SERVER['REQUEST_URI']; } ?>" alt="当前页面的QR二维码" /> |
第二种方案
1 2 3 4 5 6 7 8 9 10 11 |
function PHP_Widget_Text($text) { if (false !== strpos($text, '<?')) { ob_start(); eval('?>' . $text); $text = ob_get_clean(); } return $text; } add_filter('widget_text', 'PHP_Widget_Text'); |
将以上复制到”functions.php“里边就可以收工了。
如果你想系文章里边执行PHP代码的话,“the_content”、“the_excerpt”,自己加了。
第三种方案
先将”wp-includes\default-widgets.php“的“WP_Widget_Text”重载类直接复制到主题的”functions.php“里边。
再小修改下开头两个函数。
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 |
class PHPWidgetText extends WP_Widget { function __construct() { $widget_ops = array('classname' => 'bd_phptext_widget_text', 'description' => '我自己的 Blue Dream 主题支持执行PHP的文本框小工具' ); $control_ops = array('width' => 400, 'height' => 350); parent::__construct('bd_phptext_widget', 'PHP文本框', $widget_ops, $control_ops); } function widget( $args, $instance ) { extract($args); $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); $text = empty( $instance['text'] ) ? '' : $instance['text'] ; ob_start(); eval('?>' . $text); $text = ob_get_clean(); echo $before_widget; if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?> <div><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div> <?php echo $after_widget; } ... ... } |
最后记得系你主题的“widgets_init”里边加句
1 |
register_widget( 'PHPWidgetText' ); |