Prevent WordPress from wrapping tags with P and BRs
-
While building a new theme I found that WordPress wraps all
<samp></samp>
tags with<p></p>
. According to specs,<samp>
represents (sample) output from a program or computing system. It’s not a block-level element, it can be used inline, as well as<kbd>
,<var>
or<code>
.The problem is WordPress wraps this tag with paragraph. I managed to solve this issue with this code:
function righter_filter_ptags($content) { $content = preg_replace('/<p>\s*(<a>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content); $content = preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content); $content = preg_replace('/<p>\s*(<samp .*>*.<\/samp>)\s*<\/p>/iU', '\1', $content); return $content; } add_filter('the_content', 'righter_filter_ptags'); </a>
It actually strips Ps from images and iframes as well. While it gives the expected results – all
<img>
,<iframe>
and<samp>
tags now have no wrapping paragraphs, WordPress now adds<br>
tag before<samp>
. I’m working in ‘Text’ mode and writing everything in one line, with no line breaks. Here’s the sample code:We have not only <code>code</code> tag, but also <kbd>kbd</kbd> and <samp>samp</samp> tags.
and here’s sample output in html source code on the website:
<p> We have not only <code>code</code> tag, but also <kbd>kbd</kbd> and <br> <samp>samp</samp> tags. </p>
It looks exactly this, with all line-breaks. Everything is fine except this
<br>
tag right before<samp>
. Any ideas how to remove it? Did a lot of googling, no result.
- The topic ‘Prevent WordPress from wrapping tags with P and BRs’ is closed to new replies.