If I disable autop entirely, I need to add <p>
everywhere I want a paragraph. Is it possibe to disable autop temporarily? Like in a shortcode
2 Answers
yes, you can deactivate the autop for shortcode via filter
add_filter( 'the_content', 'shortcode_unautop' );
I do the following, which I grabbed from a plugin at some point in the past.
Add the function below to functions.php and use the two tags
<!-- noformat on -->
and <!-- noformat off -->
in your page/post editor, i.e.
text will be rendered *with* autop
<!-- noformat on -->
text will be rendered *without* autop
<!-- noformat off -->
text will be rendered *with* autop
Content outside of the two format tags will have autop enabled, as noted.
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');