【问题标题】:How to create a Wordpress shortcode-style function in PHP如何在 PHP 中创建 Wordpress 短代码样式的函数
【发布时间】:2011-06-01 21:26:09
【问题描述】:

我正在尝试在 PHP 中创建一个 Wordpress 简码样式的功能,以用图像替换像“[[133]]”这样的简码。基本上,我有一个 ID 为 1-150 的图像 URL/标题/字幕的 MySQL 表,我希望能够使用如下短代码将它们动态插入到我的页面文本中:

布拉布拉布拉布拉布拉布拉。 [[5]] 还有,bla bla bla bla [[27]] 嘿,布拉布拉布拉! [[129]]

所以,我只想获取 ID 作为 $id,然后将其提供给 MySQL 查询,例如 mysql_query("SELECT title,subtitle,url FROM images WHERE id = $id") 然后用 img/title/subtitle 替换“[[id]]”。我希望能够在同一页面上多次执行此操作。

我知道这必须涉及正则表达式和 preg_match、preg_replace、strstr、strpos、substr 的某种组合...但我不知道从哪里开始以及我应该使用哪些函数来做哪些事情。你能推荐一个策略吗?我不需要代码本身——只知道哪些部分使用什么会非常有帮助。

【问题讨论】:

  • 注意 - 这不适用于 Wordpress 网站,所以我不能使用现有的 WP 短代码功能。
  • 也许这也涉及preg_replace_callback?
  • 我冒昧地猜测一下,您可能很容易从 WordPress 中提取相关代码。或者,您可以按照 Kyle 的建议进行操作,并使用多种方法中的一种来创建您自己的解决方案。
  • 使用执行 MySQL 查询的函数 getimage($id),这 几乎 可以满足我的所有需求:[空间不足——请参阅答案]

标签: php wordpress shortcode


【解决方案1】:

我相信,正则表达式应该是:

/\[\[[1-9]{1,3}\]\]/g

(对于双括号内的 1 到 3 位数字。)

【讨论】:

    【解决方案2】:

    我的赌注是PHP's strtr函数...

    <?php
    function get_profile_image($image_url){
        return "<img src='{$image_url}' height='200px' width='200px' />";
    }
    
    $trans = array(
        "[[1]]" => "Vishal",
        "[[2]]" => "Kumar",
        "[[3]]" => "Sahu",
        "[[4]]" => "Web Designer",
        "[[5]]" => "Draw and Paint",
        "[[6]]" => ucwords("any programming language"),
        "[[7]]" => strtoupper("PHP, JAVASCRIPT and HTML"),
        "[[8]]" => get_profile_image("http://php.net/images/logos/php-logo.svg"),
        "[[9]]" => "http://php.net/images/logos/php-logo.svg"
        );
    $str = <<<HEREDOC_1
    [[8]]
    <pre>My name is [[1]] [[2]] [[3]].
    I am a [[4]] and I love to [[5]].
    I don't know [[6]] but I know [[7]] little bit.</pre>
    Here is my profile image <img src='[[9]]' alt='[[1]]-[[2]]-[[3]]-[[4]]' />
    HEREDOC_1;
    echo strtr($str, $trans);
    

    它的输出是

    [http://php.net/images/logos/php-logo.svg]我的名字是维沙尔库马尔 萨胡。我是一名网页设计师,我喜欢画画。我不知道 任何编程语言,但我对 PHP、JAVASCRIPT 和 HTML 知之甚少 少量。这是我的个人资料图片 [Vishal-Kumar-Sahu-Web Designer]

    在 5.6 上运行良好。

    【讨论】:

    • PHP 文档页面不应更改。我会添加它@MegaTron :D
    【解决方案3】:

    使用一个函数getimage($id) 执行 MySQL 查询并格式化替换文本,这几乎可以满足您的一切需求:

    $text = "Blabla [[5]] and [[111]] bla bla bla [[27]] and bla bla bla! [[129]]";
    
    $zpreg = preg_match_all('#\[\[(\d{1,3})\]\]#', $text, $matches );
    
    var_dump( $matches[1] );  
    
    $newtext = preg_replace('#\[\[(\d{1,3})\]\]#', getimage($matches[1][?????]), $text);
    
    echo $newtext;
    

    我只需要弄清楚在getimage() 中放入什么(????? 是),这将使它在正确的图像中放入正确的[[id]]

    有关详细信息,请参阅官方文档中的preg_match_allpreg_replace

    【讨论】:

      【解决方案4】:

      如果您希望能够编写这样的短代码:

      [[function_name_suffix parameter1 parameter2 ...]]
      

      这里有一个更完整的方法,使用preg_replace_callbackcall_user_func_array来实现参数化简码。

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

      如果定义了这个函数:

      function shortcode_name($firstname="",$lastname=""){
          return "<span class='firstname'>".$firstname."</span>&nbsp;<span class='lastname'>".$lastname."</span>";
      }
      

      然后这个调用

      print shortcodify("My name is [[name armel larcier]]");
      

      将输出:

      My name is <span class='firstname'>armel</span>&nbsp;<span class='lastname'>larcier</span>
      

      这只是我现在根据supertrue的想法实现的东西。

      欢迎任何反馈。

      【讨论】:

      • 哦,好吧,我认为这适用于 PHP 5.4+,因为您的代码在第一次尝试时不起作用。然后我发现你的函数名有错字。您调用了一个函数 shortcodify() 但它被定义为 shortcodify() - “i”有所不同。我为你更正了。
      • 不错的收获。非常感谢!
      【解决方案5】:

      可以为此采取各种不同的方法,具体取决于您计划如何显示等,

      取句子“Hello [34] world”

      创建一个简单的函数,例如 replaceCode($string)

      function replaceCode($string){
      
          $pos = strpos($string, '['); // Find the first occurrence of the bracket
      
          if($pos != false){
      
                // If everything is ok take the next 2 numbers from it
      
                // Check for a close bracket & remove ]
      
                // call another function to replace the number with the image text
      
          }
      
      
      
      
      }
      

      如果再发现括号,再次递归调用函数,将字符串的其余部分再次传递给函数。

      注意:可能需要先进行验证以确保 [ 和 ] 正确平衡!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-23
        • 2013-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多