是否可以暂时禁用Autop?

时间:2011-06-29 作者:JM at Work

如果我完全禁用autop,我需要添加<p> 我想在任何地方写一段。是否可以暂时禁用autop?就像在短代码中一样

2 个回复
SO网友:bueltge

是的,您可以通过过滤器停用快捷代码的自动转换

add_filter( \'the_content\', \'shortcode_unautop\' );

SO网友:markratledge

我做了以下工作,这是我在过去某个时候从插件中获取的。

将下面的函数添加到函数中。php并使用这两个标记

<!-- noformat on --><!-- noformat off -->

在页面/帖子编辑器中,即。

        text will be rendered *with* autop

        <!-- noformat on -->

        text will be rendered *without* autop

        <!-- noformat off -->

        text will be rendered *with* autop
如上所述,两个格式标记之外的内容将启用自动转换。

The function:

// <!-- noformat on --> and <!-- noformat off --> functions

function newautop($text)
{
    $newtext = "";
    $pos = 0;

    $tags = array(\'<!-- noformat on -->\', \'<!-- noformat off -->\');
    $status = 0;

    while (!(($newpos = strpos($text, $tags[$status], $pos)) === FALSE))
    {
        $sub = substr($text, $pos, $newpos-$pos);

        if ($status)
            $newtext .= $sub;
        else
            $newtext .= convert_chars(wptexturize(wpautop($sub)));      //Apply both functions (faster)

        $pos = $newpos+strlen($tags[$status]);

        $status = $status?0:1;
    }

    $sub = substr($text, $pos, strlen($text)-$pos);

    if ($status)
        $newtext .= $sub;
    else
        $newtext .= convert_chars(wptexturize(wpautop($sub)));      //Apply both functions (faster)

    //To remove the tags
    $newtext = str_replace($tags[0], "", $newtext);
    $newtext = str_replace($tags[1], "", $newtext);

    return $newtext;
}

function newtexturize($text)
{
    return $text;   
}

function new_convert_chars($text)
{
    return $text;   
}

remove_filter(\'the_content\', \'wpautop\');
add_filter(\'the_content\', \'newautop\');

remove_filter(\'the_content\', \'wptexturize\');
add_filter(\'the_content\', \'newtexturize\');

remove_filter(\'the_content\', \'convert_chars\');
add_filter(\'the_content\', \'new_convert_chars\');

结束

相关推荐