Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to stop WordPress from automatically inserting <br> tags when adding returns in the WordPress text editor.

I would like for it to behave more like a code editor where I can structure the code how I like and make it easy to read.

The code I am using in the editor is:

[one_third][team_member image_url="team_member.jpg" name="Laser vision" role="WordPress Designer"][custom_button url="#"]For more information[/custom_button][/team_member][/one_third][one_third][team_member image_url="team_member.jpg" name="Laser vision" role="WordPress Designer"][custom_button url="#"]For more information[/custom_button][/team_member][/one_third][one_third][team_member image_url="team_member.jpg" name="Laser vision" role="WordPress Designer"][custom_button url="#"]For more information[/custom_button][/team_member][/one_third]

I would like to structure it like this so it is easier to read and edit:

[one_third]
[team_member image_url="team_member.jpg" name="Laser vision" role="WordPress Designer"]
[custom_button url="#"]For more information[/custom_button]
[/team_member]
[/one_third]

[one_third]
[team_member image_url="team_member.jpg" name="Laser vision" role="WordPress Designer"]
[custom_button url="#"]For more information[/custom_button]
[/team_member]
[/one_third]

[one_third]
[team_member image_url="team_member.jpg" name="Laser vision" role="WordPress Designer"]
[custom_button url="#"]For more information[/custom_button]
[/team_member]
[/one_third]

However, when do this there will be invisible <br> tags added to the post content which will mess up the layout of my page.

share|improve this question
    
Do you want to remove the tags everywhere, or only for those shortcodes? –  bungeshea Jan 16 at 23:07

2 Answers 2

The wpautop() function adds <p> and <br> tags to your content in order to preserve line breaks. If you would rather add these tags yourself, then you can remove the filters which apply this function to the post content:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

If you would like to keep the automatic paragaraph creation (inserting <p> tags) and just remove the additional <br> tags, you can use this code instead:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

function wpse_wpautop_nobr( $content ) {
    return wpautop( $content, false );
}

add_filter( 'the_content', 'wpse_wpautop_nobr' );
add_filter( 'the_excerpt', 'wpse_wpautop_nobr' );

See this link if you're not sure where to put this code.

share|improve this answer

The answer by bungeshea is not ideal as in many cases:

  • You don't want to strip everything from <br>, <p> etc. You want it as a default behavior for your WP visual composer which the above code will delete
  • In many cases it is considered as "hacking the core" as this is changing the default core behavior of WP - for example such a thing will not pass on ThemeForest

As I can see you mainly have issues with you shortcodes. The right way to approach this is not to change the default behavior (hack the core) but to just filter the content. So just add a filter and in a variable pass an array of your shotrcodes you want to filter like this:

function the_content_filter($content) {
    $block = join("|",array("one_third", "team_member"));
    $rep = preg_replace("/(<p>)?\[($block)(\s[^\]]+)?\](<\/p>|<br \/>)?/","[$2$3]",$content);
    $rep = preg_replace("/(<p>)?\[\/($block)](<\/p>|<br \/>)?/","[/$2]",$rep);
return $rep;
}
add_filter("the_content", "the_content_filter");

The content inside will be filtered and therefore your shortcodes will be free of <br>, <p> etc. but the other parts of content - for example standard text in the WP editor created by user - will still have full functionality of WP.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.