【问题标题】:Wordpress outer shortcode parsing html tag of inner shortcode's contentWordpress外部简码解析内部简码内容的html标签
【发布时间】:2016-11-02 21:35:04
【问题描述】:

我将编写一些示例代码,因此这是我遇到的问题的简短示例。

假设我在数据库中存储了以下文本:

[form]
  <ul>
    [each name="upgrades"]
      <li><input type="checkbox" [value name="upgrade_name" id="1"] />[value name="upgrade_name" id="2"]</li>
    [/each]
  </ul>
[/form]

如果我在此文本上运行 do_shortcode,则会解析 each 内容内的短代码 INSIDE html 标签,而不是推迟到 each 短代码。但是,在 each 短代码在其内容上运行 do_shortcode 之前,不会解析不在 each 内容中的 html 标记中的短代码,这应该是正确的行为。

换句话说,ID为1value简码被解析得太快(在form简码传递中),但ID为2value简码直到each简码才被解析在其上运行do_shortcode,因此它会产生正确的值。

我知道我可以将表单简码上的 ignore_html 标志设置为 true,但这是不正确的,因为用户可能希望为简码解析 html 标记。

是否有解决此问题的方法?

Wordpress 版本 4.6.1

编辑:添加可重现的代码

使用此代码创建一个新插件:

<?php
/*
Plugin Name: Broken Shortcodes
Description: Shortcodes should not jump the gun in parsing html tag shortcodes of inner shortcode content.
*/

remove_filter('the_content', 'wpautop');

add_shortcode('form', function($atts, $content){
  echo "<textarea>This is the form's content:\n".$content.'</textarea>';
  return "<textarea>This is the rendered form shortcode:\n".do_shortcode($content).'</textarea>';
});

$bad_global_variable = 'first';

add_shortcode('value', function($atts, $content){
  global $bad_global_variable;
  return $bad_global_variable;
});

add_shortcode('each', function($atts, $content){
  global $bad_global_variable;
  $_content = '';
  foreach(array('second', 'third', 'fourth') as $v){
    $bad_global_variable = $v;
    $_content .= do_shortcode($content);
  }
  return $_content;
});



?>

使用此文本创建一个页面:

[form]
  [each]
    <div [value]>[value]</div>
  [/each]
[/form]

输出不正确:

  
<div first>second</div>

<div first>third</div>

<div first>second</div>

<div first>fourth</div>

<div first>second</div>

<div first>third</div>

<div first>second</div>
  

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    一种快速的解决方法是从 html 标签中解析出您自己的短代码。

    所以你的文字看起来像:

    [form]
      [each]
        <!-- Notice curly brackets -->
        <div {value}>[value]</div>
      [/each]
    [/form]

    那么您的每个短代码可能如下所示:

    add_shortcode('each', function($atts, $content){
      global $bad_global_variable;
      $content = preg_replace('/\{([^}]+)\}/', '[$1]', $content, -1);
      $_content = '';
      add_filter( 'wp_kses_allowed_html', 'parse_tags', 10, 2 );
      foreach(array('second', 'third', 'fourth') as $v){
        $bad_global_variable = $v;
        $_content .= do_shortcode($content);
      }
      remove_filter( 'wp_kses_allowed_html', 'parse_tags', 10, 2 );
      return $_content;
    });
    
    // This might be necessary depending on your use-case
    function parse_tags($tags, $context){
      $tags['input']['value'] = true;
      return $tags;
    }

    但是,这不应该是看似如此简单的解决方案。我认为 Shortcode API 需要一些 TLC。

    在此处查看门票:https://core.trac.wordpress.org/ticket/33134

    【讨论】:

      猜你喜欢
      • 2015-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 2012-09-23
      相关资源
      最近更新 更多