当前所在位置: 首页>>百科全书

WordPress运行代码功能非插件版

发布时间:2025-10-22 16:53:55 本站作者 【 字体: 】 浏览:109 次

网上针对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 );
        }
    }
});


上一篇:WordPress自动为文章设置特色图像

下一篇:WordPress在文章列表中插入广告

最新发布
Office2010怎么激活?附Office2010激活工具

Office2010怎么激活?附Office2010激活工具

Office2010怎么激活?附Office2010激活工具
现在很多网友在使用Office2010版本的软件,多数网友表示刚下载使用的时候是正常的,但是使用一段时间后就发现会出现激活失败的问题,那么Office2010要如何激活呢?下面就给大家分享Office2010激活的教程。在激活的过程中需要借助激活工具:1、下载好激活工具后解压并运行其中的激活程序“mini-KMS_Activator_v1.2_O...
office2016怎么激活密钥?office2016产品激活密钥安装图文教程

office2016怎么激活密钥?office2016产品激活密钥安装图文教程

office2016怎么激活密钥?office2016产品激活密钥安装图文教程
MicrosoftOffice2016是微软公司推出的一款办公软件套件。它包含了Word、Excel、PowerPoint、Outlook、OneNote、Publisher、Access等常见的办公软件,为用户提供了一个全面的解决方案。今天就让小编为大家解答一下office2016怎么激活密钥?office2016怎么安装的吧!一、如何安装office2016?office2016电脑版是款...
Office2016激活秘钥免费激活码,Office2016最新激活密钥分享

Office2016激活秘钥免费激活码,Office2016最新激活密钥分享

Office2016激活秘钥免费激活码,Office2016最新激活密钥分享
作为一款基于Windows系统的办公软件套装,Office在世界各地都不乏拥趸,中国当然也不例外,甚至还有不少企业仍在使用经典的Office2016!但想要永久破解Office2016,还需要购买最新的产品密钥或在网络中找寻尚未失效的Office2016永久激活秘钥,下面小编就来分享一些Office2016激活密钥。&nbs...
office2016永久激活密钥,office2016产品永久激活的方法(亲测可以正常激活)

office2016永久激活密钥,office2016产品永久激活的方法(亲测可以正常激活)

office2016永久激活密钥,office2016产品永久激活的方法(亲测可以正常激活)
如果要永久激活Office2016需要购买最新的产品密钥,因为网上分享的Office 2016永久激活密钥容易失效,所以要时刻关注新发布的Office2016永久激活码,或者直接购买Office2016永久序列号。这边小编为大家整理分享最新office2016永久激活密钥 office2016激活码及永久激活office2016。那么如何永久激活Office2016呢,以及激活的...