Support » Plugins and Hacks » Hacks » [Resolved] Remove unwanted BR linebreaks after Label tags

[Resolved] Remove unwanted BR linebreaks after Label tags

  • I had a problem where WordPress (or TinyMCE) adds an unwanted
    linebreak after a <label> tag in a form I put into a page, using the HTML tab of the editor. To remove them, i could use CSS or JQuery, but instead i used PHP in a quick filter for my functions.php file:

    add_filter('the_content', 'remove_bad_br_tags');
    function remove_bad_br_tags($content) {
    	$content = str_ireplace("<label><br />", "<label>", $content);
    	return $content;

    I saw this issue come up a few times in the forums, but those topics were all closed to new replies, so i wanted to share this here in case it helps someone.

Viewing 9 replies - 1 through 9 (of 9 total)
  • I had figured there was an autobr filter in WordPress, and was checking to see if there was any mention of it, when I discovered your post. Your filter worked for me with a slight modification.

    $content = str_ireplace("</label><br />", "</label>", $content);

    because the br tag was being placed after the closing label tag, rather than after the opening label tag.

    Either way thanks for the post. I got the form to display properly again a slightly modified function.

    FWIW, I believe there is also a remaining bug in TinyMCE and WordPress when using <!–nextpage–> … The closing anchor tag on the last page gets wrapped with p tags (eg <p></p>) which will cause the page not to display correctly, nor validate, as well as, throw an out of scope error. I believe a slight adaption of this function will also work for that issue.

    		add_filter('the_content', 'anvil_remove_bad_p_wraps');
    		function anvil_remove_bad_p_wraps($content) {
    			$content = str_ireplace("<p></a></p>", "</a>", $content);
    			return $content;

    I have a similar problem…

    If I put a name anchor around a heading, WP adds P and BR tags to the whole mess for some reason.

    For example, if I write the following:

    <a name="some-name"><h2>Some Heading</h2></a>

    WordPress (when using the TwentyTen theme, at least) outputs the following HTML:

    <p><a name="some-name"><br/>
    <h2>Some Heading</h2>

    This results in horrible gaps around my headings.

    Anyways, I think this thread may offer me a band-aid for this problem. Thanks.

    If I enter the following:

    <a name="css-style-blocks"></a>
    <h2>CSS Style Blocks</h2>

    WordPress outputs…

    <p><a name="css-style-blocks"></a></p>
    <h2>CSS Style Blocks</h2>

    …which doesn’t have the gap issue, but I’d rather have the anchor surrounding my heading.

    Maybe this will provide a workaround for this issue. Thanks.

    This ended up being the code I used…

    add_filter('the_content', 'remove_bad_pbr_tags');
    function remove_bad_pbr_tags($content) {
            $pattern = '/<p><a name=[\"\'](.*)[\"\']><br\s*\/>\s*<h([1-9])>(.*)<\/h([1-9])>\s*<p><\/a><br\s*\/>/';
            $replacement = "<a name=\"$1\"><h$2>$3</h$4></a>\n<p>";
    	$content = preg_replace($pattern, $replacement, $content);
    	return $content;

    I’m not too good at REGEX…so I’m sure that could be more elegant than it is…but it did match exactly what I wanted it to and replaced it with exactly with what I wanted.

    I just appended it to the end of the functions.php file in the twentyten theme.

    Actually I discovered I do not need to worry about matching the heading element itself. Also, my previous solution was not catching all cases. Below is slightly better code (it can be improved!)…which also may not catch all possible cases, but seems (so far) to catch the ones I need it to…

    // Whenever I surround a heading tag with a name anchor
    // WP adds unwanted P and BR tags, this function strips them out
    add_filter('the_content', 'remove_bad_pbr_tags');
    function remove_bad_pbr_tags($content) {
            // find any opening name anchor tags preceded by a <p> and followed by a <br /> and
            // replace them with just the name anchor itself
    	$content = preg_replace('/<p><a name=[\"\'](.*)[\"\']><br\s*\/>\s*/', '<a name="$1">', $content);
            // the closing anchor tag is followed by 2 or more new-lines in the WP editor...
            $content = preg_replace('/\s*<p><\/a><\/p>/', '</a>', $content);
            // the closing anchor tag is followed by a single new-line in the WP editor...
            $content = preg_replace('/\s*<p><\/a><br\s*\/>/', "</a>\n<p>", $content); // the double quotes are necessary in the second parameter for the \n to be considered a newline
            // the closing anchor tag is immediately followed by content (i.e., no new-lines) in the WP editor...
            $content = preg_replace('/\s*<p><\/a>[^<]/', "</a>\n<p>", $content); // the double quotes are necessary in the second parameter for the \n to be considered a newline
    	return $content;

    Okay, ACTUALLY, I randomly discovered an even better solution, which is to either set up the heading like so…

    <h2><a name="some-heading">Some Heading</a></h2>

    OR (and this is EVEN BETTER)…

    <h2 id="some-heading">Some Heading</h2>

    Either way, we can link to these bookmarks like so…

    <a href="www.somepage.com#some-heading">Some Heading</a>

    Neither of these heading setups makes WordPress add the unwanted paragraph tags to begin with!!!


    Very helpful tip, thank you everyone.

    My version of this filter worked when I accounted for an invisible new line character (\n):

    //	Strip unwanted <br /> tags from forms
    add_filter('the_content', 'remove_bad_br_tags');
    function remove_bad_br_tags($content) {
    	$content = str_ireplace("<br />\n<input", "<input", $content);
    	return $content;

    Hi itsbarry

    This seems like a solution for me…but which functions.php file do I add it to?

    I am using the Karma theme and there is a functions.php in there and one in the Karma-Child-theme folder as well….or do I put it in another one?

    Noop at this, sorry!

    neddy49 … I’m not itsbarry, but you should be able to add it to either functions.php file. However, I believe the best practice would be to add it to the functions.php in your child theme.


Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] Remove unwanted BR linebreaks after Label tags’ is closed to new replies.