WordPress教程

wordpress运行代码功能非插件完美版

阿里云

运行代码功能估计需要的人不多,但是博客写前端代码的人应该就需要,因为能够预览代码这样子体验非常好。网上针对 WordPress 运行代码的插件和非插件版的方法都已经过时了,最主要的问题还是加入运行框中的代码会自动插入换行符,这直接导致了代码无法运行。网上有很多这样子的例子。但是基本上都是错误的,反正我没要找到正确的,主要原因就是 wordpress 会自动添加分段标签 p,br 等等,导致代码不能够预览,相信大家都对这个 wordpress 自动机制感到很绝望,下面我们来看看怎么来做,下面这个方法完美解决的这些问题。

一、首先准备 JS 代码:

JS 函数控制运行代码按钮和全选按钮。

也想出现在这里?联系我们
创客主机
  1. //JS代码
    
  2. function runCode(objid) {
    
  3. 	var winname = window.open('', "_blank", '');
    
  4. 	var obj = document.getElementById(objid);
    
  5. 	winname.document.open('text/HTML', 'replace');
    
  6. 	winname.opener = null; 
    
  7. 	winname.document.write(obj.value);
    
  8. 	winname.document.close();
    
  9. }
    
  10. function selectCode(objid){
    
  11.     var obj = document.getElementById(objid);
    
  12.     obj.select();
    
  13. }

二、functions 代码:

网上的大多数办法,都没法逃避 wordpress 的自动过滤机制,如果代码预览中 js 存在 html 标签,那么运行代码将失效,经过我多次研究,写了一个自动替换的短代码方案,这个实在后期进行匹配过滤的,完全可以所见所得。

  1. $RunCode = new RunCode();
  2. add_filter('the_content', array(&$RunCode, 'part_one'), -500);
  3. add_filter('the_content', array(&$RunCode, 'part_two'),  500);
  4. unset($RunCode);
  5. class RunCode
  6. {
  7.     var $blocks = array();
  8.     function part_one($content)
  9.     {
  10.         $str_pattern = "/(\<runcode(.*?)\>(.*?)\<\/runcode\>)/is";
  11.         if (preg_match_all($str_pattern, $content, $matches)) {
  12.             for ($i = 0; $i < count($matches[0]); $i++) {
  13.                 $code = htmlspecialchars($matches[3][$i]);
  14.                 $code = preg_replace("/(\s*?\r?\n\s*?)+/", "\n", $code);
  15.                 $num = rand(1000,9999);
  16.                 $id = "runcode_$num";
  17.                 $blockID = "<p>++RUNCODE_BLOCK_$num++";
  18.                 $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.'\')"/>';
  19.                 $this->blocks[$blockID] = $innertext;
  20.                 $content = str_replace($matches[0][$i], $blockID, $content);
  21.             }
  22.         }
  23.         return $content;
  24.     }
  25.     function part_two($content)
  26.     {
  27.         if (count($this->blocks)) {
  28.             $content = str_replace(array_keys($this->blocks), array_values($this->blocks), $content);
  29.             $this->blocks = array();
  30.         }
  31.         return $content;
  32.     }
  33. }

参考 CSS:

  1. .runcode{
  2. width: 100%;
  3. font-size:13px;
  4. padding:10px 15px;
  5. color:#eee;
  6. background-color: #263540;
  7. margin-bottom:5px;
  8. border-radius:2px;
  9. overflow:hidden
  10. }

运行方法

在撰写文章时切换到 html 模式,输入以下标签即可:

  1. <runcode>//这里贴要运行的代码</runcode>

wordpress 运行代码功能非插件完美版

已有 435 人购买
查看演示升级 VIP立刻购买

收藏
(3)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!