如何创建一个什么都不做的 Wordpress 简码? [英] How to create a Wordpress Shortcode that does nothing?

查看:35
本文介绍了如何创建一个什么都不做的 Wordpress 简码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个用作包装器的简码,以便使 wpautop() 函数生效.

I'm trying to create a Shortcode to use as a wrapper so as to deflect the wpautop() function from coming into effect.

是的,我知道如何完全删除自动格式,但目标是创建一个自定义的短代码,我可以将 <img 标记并在特定情况下绕过自动格式.

Yes, I know how to remove the auto formatting in it's entirety but the goal is to create a custom Shortcode that I can wrap around an < img tag and circumvent the auto format in select cases.

我已经创建了这个简码:

I've created this Shortcode :

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

但是当应用时,图像就会消失.

But when applied the image simply disappears.

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

How can I create a Shortcode that has a null effect (does nothing to the wrapped element)?

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

[nowpautop] img tag here [/nowpautop]

这个问题是这个问题的延伸:在 Wordpress 中创建禁用 wpautop() 包装内容的简码? 因为我认为这个问题更切中要害.

This question is an extension of this one: Create shortcode in Wordpress that disables wpautop() on wrapped content? as I believe this question is more to the point.

推荐答案

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

这篇关于如何创建一个什么都不做的 Wordpress 简码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆