如何创建一个什么都不做的 Wordpress 简码?

How to create a Wordpress Shortcode that does nothing?

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

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

我创建了这个简码:

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

但是应用后图像就消失了。

如何创建一个具有空效果的短代码(对包装元素不做任何事情)?

[nowpautop] img 标签在这里[/nowpautop]

这个问题是这个问题的延伸: 因为我认为这个问题更切题。

已解决

简码替换内容,因此解决方案使用属性将包装元素移动为源,并使用简码包装器设置开始和结束

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

在 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]

最终结果: < h3 style="display:inline;">图形优势 < img 样式="max-width: 50px; width: 100%;" src="https://sgdesign.com/images/SGDadvantage.png" alt="SGD Advantage" />

因为 < h3 的样式为 "inline" 图形现在位于实现主要目标的同一条线上。

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++];
    });
});