WordPress运行代码功能非插件版
发布时间:2025-10-22 16:53:55 本站作者 【 字体:大 中 小 】 浏览:55 次
网上针对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 );
}
}
}); 热门推荐
广告位-300PX*250PX
最新发布
四季肥肠的制作方法
四季肥肠是广东地区非常有名的传统特色美食,深受广大食客的喜爱。它由猪肠、猪蹄、猪肉、花生等多种材料制作而成,口感鲜嫩、细腻,香气扑鼻,食后回味无穷。如果您也想在家里尝试制作这道美食,不妨跟随下文的步骤一起动手吧!1.准备材料和工具在制作四季肥肠之前,我们需要准备好一些必要的材料和工具,例如猪肠、猪蹄、...
15种口味任你选,手把手教你做
三明治是西方国家非常流行的一种食物,而以吐司作为外壳的三明治更加受到人们的喜爱。今天,我将为大家介绍如何用吐司制作美味三明治,并分享15种不同口味的做法,让您不再局限于快餐店的单调选择。步骤1准备所需材料准备吐司、火腿肠、生菜、番茄、黄瓜、鸡蛋、奶酪、黄油、酱汁等材料。步骤2切好材料将火腿肠、番茄、黄瓜...
油条油馍制作全攻略
早餐是一天中最重要的一餐,而油条油馍则是中国人喜爱的早餐食品之一。自己动手做油条油馍,不仅可以享受到美味的口感,还能够保证食品的卫生和安全。本文将为大家介绍以油条油馍的制作方法和注意事项。准备工作:选购原材料准备好面粉、酵母、盐、白糖、清水、油条所需的豆腐乳、芝麻等食材,并确认这些食材的新鲜程度和卫...
宝宝食谱之水饺肝粉油菜猪肉胡萝
宝宝的饮食非常重要,营养的摄取对于宝宝健康成长至关重要。本文将为大家介绍一道适合宝宝的菜谱——水饺肝粉油菜猪肉胡萝卜。一、准备食材:蒸馏水、冷冻水饺、鲜猪肉、新鲜胡萝卜、新鲜油菜、新鲜鸡肝在准备食材时,建议选择新鲜、干净卫生的食材,确保食材的安全和卫生。二、清洗水饺和蔬菜在开始制作菜品前,需将水饺和...






