I’m relatively new to WP and am trying to migrate some existing forms to a newly created WP site. I’m finding that WP inserts empty p tags and/or br tags if I just copy/paste because of indentations, etc., I made in my original code. I took all of the extra space out and got rid of most of the problems but WP is still consistently adding a new line before every select tag. I’ve tried deactivating all plugins and changing themes and nothing is working so far. I’ve Googled this problem and have found several references to people with similar issues - mostly a few years back. People have suggested various fixes but none of them have seemed to work consistently for others and there’s been disagreement about whether the fixes are wise. What’s even more of a concern to me is that I haven’t been able to find anyone who seems to know what’s causing it. I’m reluctant to apply a fix when I don’t understand the cause.
Basically, I’m trying to add simple code like this:
Any ideas what may be happening and/or how to fix it? I don’t know if this matters but the themes I’ve tried this on are Twenty Eleven, Twenty Twelve, Twenty Thirteen, Twenty Fourteen and Weaver. I’m getting the extra tags inserted on all of them.
Wordpress’ wpautop() function that adds the paragraph tags to content is buggy. A lot of people have had problems with it including myself. As far as I can tell, the common alternative is to disable it using a filter and have another filter create <br> tags using nl2br() to simulate paragraphs. That eliminated the problem and also eliminated all the <p> tags. You may not want that as you no longer have the semantic meaning of the <p> tag (which may not be a big deal to you anyway).
This is the code I used to create a plugin (using existing examples as guidance). Stuck it in PHP file and uploaded it to my plugins folder then activated the plugin in the Wordpress admin panel.
<?php
/*
Plugin Name: Remove Auto Paragraph Tags
Description: This plugin removes Wordpress' buggy auto paragraph formatting.
*/
// Remove auto paragraph tag wrapping from post content and turn on conversion of newlines to HTML linebreak tags (<br />).
remove_filter('the_content','wpautop');
add_filter('the_content', 'nl2br');
// Remove auto paragraph tag wrapping from excerpts and turn on conversion of newlines to HTML linebreak tags (<br />).
remove_filter('the_excerpt', 'wpautop');
add_filter('the_excerpt', 'nl2br');
?>
As I said, you may not want to do that as it will eliminate all auto generated <p> tags. I had an idea for a more complex plugin method but did not pursue it as I have other things to occupy my time.
If I completely disable wpautop, I’ll still be able to add my own p tags, right? That might be an acceptable alternative unless the theme relies on wpautop for some of what it does. I don’t mind being responsible for putting in all my own p tags in content but it could get tricky if the theme is using wpautop in places other than user content areas. I’m using Twenty Eleven. Am I likely to run into problems with that?
Yes, using the text editor tab in the post screen you can add your own <p></p> tags.
As far as I saw, the <p> tag issue was in the post and excerpt content.
After I posted earlier this morning, I thought about if it would be possible to create a custom post type and then run the filters to disable the auto <p> tags for only those custom posts. Then you could have auto <p> tags across your site then disable it for only certain types of posts where it is a problem. I did not consider it long and do not have time to investigate it now. But you can look into it.
Possibly, you could create a custom shortcode then have a function disable auto <p> tags when that shortcode is present. Just ideas off the top of my head. And I just found this, which looks like a very nice solution to your problem:
By placing a function in your template file (you could probably do this in a plugin as well), you can put a code in a custom field in your post and disable auto <p> for posts with that code in a custom field. That looks interesting.