WordPress运行代码功能非插件版
发布时间:2025-10-22 16:53:55 本站作者 【 字体:大 中 小 】 浏览:101 次
网上针对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 );
}
}
}); 热门推荐
最新发布
支付宝扫雷群禁抢群(2026最新)24小时在线
全网支付宝扫雷群埋雷群禁抢群 全天拉手中介在线微信。全部是微信聊呗以前的群搬过去开的群!QQ2100756172欢迎加入信誉...
office365激活密钥【最新】Office 365永久激活密钥2026年最新激活码
Office365激活密钥是很多用户都在找的,没有密钥激活你的Office365你是无法使用的,下面小编通过一些渠道收集了最新的Office365激活密钥,也是Microsoft365激活密钥分享给大家,需要知道的是Office365激活密钥有一定的时效性,要赶紧使用!不然会过期哦。Office365激活密钥【最新】45H17-8514D-E4758-TH015-4V1510H451-87K0...
office2021最新激活秘钥,2026年最新永久有效正版激活密钥分享
office是我们如今生活中经常用到的电脑软件,但是有不少朋友们的office软件由于没有激活无法正常使用。今天小编就来给大家**分享一下2021最新永久有效正版密钥,这些密钥适用于office365、office专业增强版、office2016等多系列。雨林木风 ghost Win11 64位 超级精简版系统 v2022.06是一款很多用户选择的win11系统,推出之后...
office2021激活工具,2026年office2021激活工具及激活密钥分享
office2021激活工具其实就是我们通用的HEU KMS Activator软件,office2021激活工具是一款支持微软最新办公软件office2021的一键激活工具,它能够迅速而高效地破解产品的所有受限功能,使用户尽享全面的办公体验。该工具兼容性极高,支持多种操作系统,并且除去繁琐的手动激活步骤,让用户轻松完成激活过程。无论是个人用户还...
大家都在看
支付宝扫雷群禁抢群(2026最新)24小时在线
office365激活密钥【最新】Office 365永久激活密钥2026年最新激活码

office2021最新激活秘钥,2026年最新永久有效正版激活密钥分享

office2021激活工具,2026年office2021激活工具及激活密钥分享

Office2024激活密钥分享,2026年Office 2024专业增强版密钥分享

visio 2016 永久激活密钥 visio 2016专业版激活码 正版 visio 2016 零售版产品密钥

office2016专业增强版密钥,office2016专业版激活秘钥分享(亲测可用)

Office2016激活密钥,Office2016专业版/家庭和学生版密钥

2026最新office2016专业版永久激活密钥/序列号,office2016专业版激活秘钥分享推荐

office2016专业增强版永久激活密钥,office2016专业版激活秘钥怎么用

