WordPress运行代码功能非插件版
发布时间:2025-10-22 16:53:55 本站作者 【 字体:大 中 小 】 浏览:4 次
网上针对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
最新发布

wordpress长文章内容分页及通用分页样式

一般wordpress对长文章内容分页可以在文本html编辑器状态下,在需要分页的位置加入<!--nextpage-->即可,这是wordpress本身内置的功能,但某些主题却无法实现,说明你所使用的主题不支持这个功能,要实现也很简单,需要先在主题的single.php内找到下面这行代码:<?phpthe_content();?>然后在...

wordpress安全-防暴力破解终极方法

今天打开邮箱时,又发现了有人在暴破网站后台想想以前重命名wp-login.php之后就再也没发生过什么,不过当时需要前台登录而且前台无法登出,又改了回去。后来换了一种方式就是用跳转代码,相当于隐藏了后台菊花,没想到还是有人能直接找到。索性来了个终极防护手段:重命名wp-login.php ,并且对其跳转。特别注意,以下方法仅...

WordPress屏蔽/禁止指定IP或IP段(Linux主机)

今天看统计的时候,偶然间看到一个ip,发现这个家伙不断尝试查看我的网站的一些隐私目录,当然一般情况下是看不到什么的,但这怎么说总让人很不舒服啊,就像有个人一直趴在窗户上盯着你家里看一样,就算他进不来 也得赶一赶吧?!下面是一个很简单的wordpress 禁止/屏蔽某IP或IP段的方法。只需要修改.htaccess文...

WordPress 出现 Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM 的解决办法

今天在本地测试whatnew主题时,启用后出现 Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM 的提示,Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in E:phpnow-1.5.6PHPnow_33lc.comhtdocswp-contentthemeswhatnew-wplibmeta-boxmeta-boxincmeta-box.php on line 122网上一查php的 syntax...