WordPress运行代码功能非插件版
网上针对WordPress 运行代码的插件和非插件版的方法都已经过时了,最主要的问题还是加入运行框中的代码会自动插入换行符,这直接导致了代码无法运行。下面这个方法完美解决的这些问题。
加入functions.php
自定义函数,完美解决换行符的问题,所贴即所见。注意:请将代码中的中文书名号改为尖括号。
$RunCode = new RunCode();
add_filter('the_content', array(&$RunCode, 'part_one'), -500);
add_filter('the_content', array(&$RunCode, 'part_two'), 500);
unset($RunCode);
class RunCode
{
var $blocks = array();
function part_one($content)
{
$str_pattern = "/(《runcode(.*?)》(.*?)《/runcode》)/is";
if (preg_match_all($str_pattern, $content, $matches)) {
for ($i = 0; $i < count($matches[0]); $i++) {
$code = htmlspecialchars($matches[3][$i]);
$code = preg_replace("/(s*?r?ns*?)+/", "n", $code);
$num = rand(1000,9999);
$id = "runcode_$num";
$blockID = "<p>++RUNCODE_BLOCK_$num++";
$innertext='<h3>代码预览</h3><textarea id="'.$id.'" class="runcode">'. $code . '</textarea><input type="button" value="运行代码" onclick="runCode(''.$id.'')"/><input style="margin-left: 47px;"type="button" value="全选代码" onclick="selectCode(''.$id.'')"/>';
$this->blocks[$blockID] = $innertext;
$content = str_replace($matches[0][$i], $blockID, $content);
}
}
return $content;
}
function part_two($content)
{
if (count($this->blocks)) {
$content = str_replace(array_keys($this->blocks), array_values($this->blocks), $content);
$this->blocks = array();
}
return $content;
}
}JS函数控制运行代码按钮和全选按钮
function runCode(objid) {
var winname = window.open('', "_blank", '');
var obj = document.getElementById(objid);
winname.document.open('text/html', 'replace');
winname.opener = null;
winname.document.write(obj.value);
winname.document.close();
}
function selectCode(objid){
var obj = document.getElementById(objid);
obj.select();
}参考CSS
.runcode{
width: 100%;
font-size:13px;
padding:10px 15px;
color:#eee;
background-color: #263540;
margin-bottom:5px;
border-radius:2px;
overflow:hidden
}运行方法
在撰写文章时切换到文本模式输入以下标签即可
<runcode>//这里贴要运行的代码</runcode>
希望textarea高度自适应而不出现滚动条
网上有css方法,没有效果,故采用jquery方法实现
$(function() {
$('.mAIn-content textarea').autoHeight();
});
$.fn.extend({
autoHeight: function(){
return this.each(function(){
var $this = $(this);
if( !$this.attr('_initAdjustHeight') ){
$this.attr('_initAdjustHeight', $this.outerHeight());
}
_adjustH(this).on('input', function(){
_adjustH(this);
});
});
//重置高度
function _adjustH(elem){
var $obj = $(elem);
return $obj.css({height: $obj.attr('_initAdjustHeight'), 'overflow-y': 'hidden'}).height( elem.scrollHeight );
}
}
});您可能感兴趣的文章
- 11-11wordpress导航菜单新窗口打开的设置方法
- 11-11WP-PostViews插件如何修改文章阅读数
- 11-11wordpress附件保存目录改为年月日和上传文件重命名为时间戳
- 11-11WordPress 上传媒体库文件重命名 全格式支持
- 11-11解决wordpress安装后更新或者上传文件权限不足的问题
- 11-11wordpress实现主动推送+熊掌号推送同步进行
- 11-11WordPress 添加友情链接设置 nofollow 属性
- 11-11WordPress 实现 wp_list_bookmarks 自定义友情链接排除调用
- 11-11WordPress首页指定或排除某分类文章显示
- 11-11WordPress不同分类调用不同的文章模板


阅读排行
推荐教程
- 09-22wordpress如何添加描述
- 11-11wordpress附件保存目录改为年月日和上传文件重命名为时间戳
- 10-19纯代码给WordPress网站添加独立下载页面功能
- 11-11WordPress之给文章内容中间插入广告的实现方法
- 10-18wordpress无插件调用随机文章的方法
- 10-18WordPress 技巧:WordPress 后台也使用七牛云存储
- 10-24Win10无法调节声音怎么办?Win10无法调节声音的解决方法
- 10-19WordPress上传图片HTTP错误的解决方法
- 10-18让注册用户可以上传自己的头像的WordPress插件-WP User Avatar v2.
- 11-11国外WordPress精美主题如何DIY汉化成中文版呢?





