【问题标题】:wordpress style shortcodes in Ckeditor / PHPCkeditor / PHP中的wordpress样式短代码
【发布时间】:2015-02-12 20:34:55
【问题描述】:

我已经构建了一个自定义 CMS。最近,我添加了动态创建页面的功能。这些页面的内容我使用了 CKEditor。 我还想运行一些 php 函数,这些函数可能包含在存储在 mysql 中的页面内容中。 我不想将实际的 PHP 代码存储在数据库中,而可能是函数名。例如,在我可能拥有的数据库中存储的页面中。

<?php //begin output
hello world!
check out this latest news article.
news($type, $id);
//end output 
?>

如果在输出中找到并执行此现有函数而不使用 EVAL,那么最好的方法是什么?我在考虑 wordpress 风格的短代码。也许 [[news(latest, 71]] ?然后有一个函数来查找并执行这些函数,如果它们存在于我的 functions.php 文件中。不太确定最好的方法。 我不是在寻找任何代码答案,而是针对这种情况的最佳实践,尤其是对可能的注入最安全的情况。

【问题讨论】:

    标签: php mysql function ckeditor


    【解决方案1】:

    我通过挖掘并找到此线程找到了解决方案 How to create a Wordpress shortcode-style function in PHP

    我可以在 CKEditor 中传递这样的短代码

    [[utube 1 video_id]]
    

    然后,在我呈现代码的页面中:

    print shortcodify($page_content);
    

    使用此功能:

    function shortcodify($string){
        return preg_replace_callback('#\[\[(.*?)\]\]#', function ($matches) {
        $whitespace_explode = explode(" ", $matches[1]);
        $fnName = array_shift($whitespace_explode);
        return function_exists($fnName) ? call_user_func_array($fnName,$whitespace_explode) : $matches[0];
        }, $string);
        }
    

    如果函数名称存在(utube),它将触发该函数。 我目前唯一的问题与我将 [[shortcode]] 放在编辑器中的哪个位置无关,它总是首先执行。 例如,我在 CKEditor 中输入:

    Hello world! Check out my latest video
    [[utube 1 video_id]]
    

    它将始终将文本放在视频下方,而不是在文档中的位置。我需要想办法让短代码按照放置的顺序执行。

    【讨论】:

      猜你喜欢
      • 2013-02-23
      • 2013-11-24
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 2023-03-12
      相关资源
      最近更新 更多