Fvcode 帖代码的小插件
2008.06.12 4:10 下午 WordPress
呵呵,这只是实验性的,也根本没有实际用途,功能也过于单一,不过我还是觉得够用了。
一直想找一个小巧的灵活的帖代码插件,但一直没有找到,于是今天花了一个小时研究了一下,写出了这个小插件。
使用他很方便只要插入:
<code><?php echo $test; ></code>
效果演示:
/* 我就是演示了 */ <?php phpinfo(); ?> <html> <!-- test --> </html>
就能显示代码了,这也符合我平时的习惯,还很好的向以后更换插件做好了兼容工作,如果像使用
之类的特殊符号,那以后会很麻烦的。
因为太小所以就不放下载了
直接帖出代码,下面是PHP代码和CSS样式表
<?php
# -*- coding: utf-8 -*-
/*
Plugin Name: FVcode
Plugin URI: http://sc35.com/blog/fvcodefvcode.fv
Description: 这是一个小东西
Author: FV.zone
Version: 0.0.0.2
Author URI: http://sc35.com/blog/
*/
require_once(ABSPATH.'wp-config.php');
require_once(ABSPATH.'wp-settings.php');
function fvcode_style()
{
$css_url = get_bloginfo("wpurl") . '/' . PLUGINDIR . '/' . dirname(plugin_basename (__FILE__))."/fvcode.css";
echo "\n".'<link rel="stylesheet" href="' . $css_url . '" type="text/css" media="screen" />'."\n";
}
function fvcode_code($match)
{
foreach ($match as $value) {
$value = htmlspecialchars($value);
$content = nl2br($value);
}
$o = '<div class="fvcode">';
$o .= '<div class="code">'.$content.'</div>';
$o .= '</div>';
$o .= '<div class="clear"></div>';
return $o;
}
function fvcode($content) {
$content = preg_replace_callback("/\s*<code>\s*(.+?)\s*<\/code>\s*/sim", 'fvcode_code', $content);
return $content;
}
function fvcode_format($content) {
$content = str_replace("–", "--", $content);
return $content;
}
add_action('wp_head', 'fvcode_style');
add_filter ('the_content','fvcode_format');
add_filter ('the_excerpt','fvcode_format');
add_filter ('comment_text','fvcode_format');
add_filter('the_content', 'fvcode',9);
add_filter('the_excerpt', 'fvcode',9);
add_filter('comment_text', 'fvcode',9);
?>
你只要将这个代码复制保存命名随便,然后建立一个文件夹或是不建立也行,放到plugins目录里,在后台激活就可以了。
CSS样式:
/*
coding: utf-8
*/
.clear{clear:both;}
.fvcode {
margin:6px 0;
font:12px "Lucida Grande",Verdana,Arial,Sans-Serif;
text-align:left;
}
.code {
padding:6px;
float:left;
line-height:18px;
background:#ECF6FF;
color:#666;
border:1px solid #D6E9FC;
display: inline;
}
CSS要把保存的名字命为:fvcode.css才行,当然你也可以改掉PHP里面的定义,这个随便你。