【问题标题】:How to create a Wordpress Shortcode that does nothing?如何创建一个什么都不做的 Wordpress 简码?
【发布时间】:2018-03-17 18:17:59
【问题描述】:

我正在尝试创建一个短代码用作包装器,以使 wpautop() 函数无法生效。

是的,我知道如何完全删除自动格式,但目标是创建一个自定义短代码,我可以将其包裹在

我已经创建了这个简码:

function no_wp_autoformat_shortcode() {
    return null;
}
add_shortcode('nowpautop', 'no_wp_autoformat_shortcode');

但应用后,图像就会消失。

如何创建一个具有 null 效果的简码(对包装的元素没有任何作用)?

[nowpautop] 此处为 img 标签 [/nowpautop]

这个问题是这个问题的延伸: Create shortcode in Wordpress that disables wpautop() on wrapped content? 因为我相信这个问题更重要。

【问题讨论】:

    标签: wordpress shortcode


    【解决方案1】:

    已解决

    短代码替换内容,因此解决方案使用属性将包装的元素转换为源,并使用短代码包装器设置开始和关闭

    如果有人想要解决类似的案例,这里有一个工作示例。注意功能和短代码名称应更改为更合适的名称。

    在functions.php(子)中创建简码

    function no_wp_autoformat_shortcode( $atts, $content = null ) {
    
        return '<img style="max-width: 50px; width: 100%;" src="'. $content .'" alt="SGD Advantage"  />';
    }
    
    add_shortcode('nowpautop', 'no_wp_autoformat_shortcode');
    

    在页面上使用:

    <h3 style="display:inline;">Graphic Advantage</h3>
    [nowpautop]https://sgdesign.com/images/SGDadvantage.png[/nowpautop]
    

    最终结果:

    图形优势

    由于

    【讨论】:

      【解决方案2】:
      add_action( 'init', function() {
          add_filter( 'the_content', function( $content ) use ( &$matches ) {
              # save content between nowpautop tags, priority 9 means that this will run before wpautop
              preg_match_all( '#\[nowpautop\](.*?)\[/nowpautop\]#', $content, $matches, PREG_PATTERN_ORDER );
              # this filter returns $content unchanged and is run only to save the original content between nowpautop tags
              return $content;
          }, 9 );
          add_shortcode( 'nowpautop', function( $atts, $content ) use ( &$matches ) {
              # restore the original content between nowpautop tags
              static $index = 0;
              return $matches[1][$index++];
          });
      });
      

      【讨论】:

      • 完全按需要工作,在短代码中包装
      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 2021-02-17
      • 2012-08-30
      • 2013-12-27
      • 2014-01-01
      • 2018-12-08
      相关资源
      最近更新 更多