I'm not sure if is wordpress itself or Tiny MCE that's doing it. But when switching between HTML and Visual editor it's adding
, and adding an extra one each time I switch back and forth. I don't even have a line break in the code, it seems to somehow be deciding where it wants to place them based on the tags in my html code.
What's causing this and how do I fix it?
I've already tried this in the template functions file:
//disable auto p
remove_filter ('the_content', 'wpautop');
remove_filter ('comment_text', 'wpautop');
// Remove auto formatting
remove_filter('the_content', 'wptexturize');
remove_filter('comment_text', 'wptexturize');
remove_filter('the_title', 'wptexturize');
I'm not sure if is wordpress itself or Tiny MCE that's doing it. But when switching between HTML and Visual editor it's adding
, and adding an extra one each time I switch back and forth. I don't even have a line break in the code, it seems to somehow be deciding where it wants to place them based on the tags in my html code.
What's causing this and how do I fix it?
I've already tried this in the template functions file:
//disable auto p
remove_filter ('the_content', 'wpautop');
remove_filter ('comment_text', 'wpautop');
// Remove auto formatting
remove_filter('the_content', 'wptexturize');
remove_filter('comment_text', 'wptexturize');
remove_filter('the_title', 'wptexturize');
Share
Improve this question
asked Sep 27, 2011 at 19:54
DaveDave
8104 gold badges9 silver badges21 bronze badges
5
- 2 It's actually doing this automatically when you switch tabs, I noticed it too, and found it very annoying. No clue on how to fix it yet though... – Joost de Valk Commented Sep 28, 2011 at 15:17
- 1 I use the HTML editor almost exclusively, pasting code in there from BBEdit. It seems to do this to me when I save in WP. – Goodbye Stack Exchange Commented Oct 25, 2011 at 19:30
- well I have this problem too. When I post manually and click enter button, it will add paragraph for me. This happens after i installed tiny mce. What do you mean by this anyway: Filter tiny_mce_init, to override the default content-filtering rules – user15672 Commented Apr 30, 2012 at 10:19
- 1 This is happening to me on post titles in the loop. Seems indiscriminate (happens on only some space characters of some posts). – Drewdavid Commented Nov 23, 2016 at 5:26
- I managed to find an easy workaround for this issue: wordpress.stackexchange/a/291746/135285 – AturSams Commented Jan 21, 2018 at 19:38
6 Answers
Reset to default 2All I use is remove_filter( 'the_content', 'wpautop' ); remove_filter( 'the_excerpt', 'wpautop' );
and that stops Wordpress from creating any extra markup. Are you copying and pasting your code from an external editor?
I found that non-breaking spaces are being inserted when I use two spaces after a period. The editor seems to interpret the extra space as an attempt on my part to use spaces for text formatting.
If I copy-and-paste my article from the Wordpress editor into a text editor that supports hex display (I use EditPad Pro), I see that text that I thought is a period followed by two spaces is in reality text followed by a single space, followed by the unicode character with the hex code A0, which is the Unicode NO-BREAK SPACE character.
I was able to remove the non-breaking space in existing articles by replacing space followed by the NO_BREAK SPACE character by just a space. In most text editors, you should be able to do that with search & replace by copying the "two spaces" into the Find text box and writing a single space into the Replace text box.
I was able to avoid the issue going forward by only typing a single space after a period.
What's causing this and how do I fix it?
Any content added to or removed from your Post Content is caused by tiny_mce_init
, which is applying is's content-filtering rules.
The ways to prevent it are:
- Filter
tiny_mce_init
, to override the default content-filtering rules - Stop switching between Visual and HTML editors. Really; unless you want to use option #1 above, don't switch between editors. WordPress is intended to be used such that content is entered using the Visual or the HTML editor, but not both simultaneously.
Example use for the tiny_mce_before_init
filter in Codex. Another example of TinyMCE configuration/customization in Codex here.
In case there is somebody still stuck with this in 2020. We had this issue after migrating a bunch of content to Wordpress. Guess what, everybody did the copy and paste thing and we end up with way too much content to be able to fix manually. I implemented the php function bellow and added it to functions.php and it solved the issue for us.
function replace_content($content) {
$content = htmlentities($content, null, 'utf-8');
$content = str_replace(" ", " ", $content);
$content = html_entity_decode($content);
return $content;
}
add_filter('the_content','replace_content', 999999999)
The 999999 is just to make sure this filter will run last, in case these
are being added somewhere in the middle.
I'm using Wordpress for a Hebrew Blog.
Workaround: Copy the text into an editor like sublime or notepad++ and search and replace the with normal spaces. You can copy one of them from the text.
Disclaimer: this is not much of an answer and more of a workaround so feel free to downvote. For me Wordpress is happily adding nbsp everywhere and without rhyme or reason. Not sure what is causing it. It is happening with Hebrew text a lot, much more than with any English text.
In css file. Put display: inline-flex
to the p inside the excerpt
. The additional white spaces don’t bother anymore. works for me