Web Log
Feedback
Recent Posts
- I Just Wanted an Electric Typewriter
- Easy Plain Text WordPress (Look Ma! No curly quotes!)
- MySpace Layouts Update
- Resize Pics Code Update
- Profile Tweaker Update
- MySpace Layouts Updates
- Attn: Link Removal Licensees
- Firefox 2.0.0.6 update
- Super EZ Profile Tweaker
- Link Removal License
- New Plain MySpace Layouts
- Plain Black MySpace Layouts
- May 2009
- May 2008
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- January 2007
- November 2006
- October 2006
- September 2006
All the archives to my old web blog can be found here.
Easy Plain Text WordPress (Look Ma! No curly quotes!)
Have you ever wrestled interminably with a WordPress blog post trying to get it formatted exactly the way you want it?
And it just won't bleepin' behave?
Heh heh, I feel your pain.
I know HTML like the back of my hand. I know exactly how I want my blog post to be formatted. So I enter the appropriate code using the code tab on the rich text editor (a.k.a., WYSIWYG editor). Click 'Save' and view my post.
And what happens?
Yeah you know it...
The formatting gets all messed up. WordPress decides it's going to do things ITS way and:
- Remove your
<br>tags. - Mysteriously add empty paragraphs, e.g.,
<p></p> - Parse all your character entities or just disappear them into some kind of virtual black hole —thereby making it impossible to post example codes.
...and generally get you so riled up trying to have things your way and not getting it that you're ready to toss your computer off a cliff. (And then climb down the cliff with a sledgehammer and beat the wreckage to a pulp.)
Now there are a couple or three good WordPress plugins out there that will solve all this —TextControl being one of them.
However —and with all due respect to these authors— I found some of these plugins to be a little heavy-handed for my tastes. I don't need a whole gaggle of options. I know exactly what I want to do (get WordPress to post my HTML exactly how I coded it) and, moreover, I want solutions that are easy to understand.
Then whenever WordPress pumps out yet another security fix, reimplementing my tweaks will be a simple matter and will not be dependent on any plugin authors making sure all 4 bazillion lines of code in their PHP plugin files are compatible with the latest version of WordPress.
So, after Googling extensively and searching far and wide through the WordPress forums, here is what I came up with to put a clean and simple leash on WordPress and make it do the following:
- Stop parsing my example codes.
- Stop fancifying my plain quotes and double quotes into those damnable fancy curly quotes.
And above all... - Stop pooping all over my raw HTML code, sweeping it aside and then formatting everything into its tunnel vision idea of paragraphing and creating line breaks.
In other words, display my blog post using the exact HTML I entered on the 'Write Post' screen.
Here's what I did to accomplish this:- First of all, I disabled the WYSIWYG editor. To do this, you have to go to Users >> Your Profile and simply clear the checkbox beside "Use the visual editor when writing". Then click on 'Update Profile' to apply the changes.
Done and done.
Next... - I installed Jason Litka's Disable Texturizer plugin. This will prevent WordPress from fancifying a lot of your input like turning plain double quotes "..." into curly double quotes “...” and the like. This great little plugin is only 3 lines of code. All it does is disable the wptexturize filter for your posts, excerpts and comments. Very simple, very clean. Easy to install.
And finally.. - I hacked into the WordPress
formatting.phpfile just a wee bit. To do this, go to thewp-includessubfolder in the folder where your WordPress blog is installed, open theformatting.phpfile in a plain text editor and do a search for "function wpautop" which will bring you to this line of code:
function wpautop($pee, $br = 1) {
Directly after that type in (or copy and paste) these two lines of code:
// Disable WP auto-paragraphing on call
if ( preg_match('/<!--PLAIN_TEXT-->/', $pee) ) return $pee;
The rest of the wpautop function should remain as is.
Example:
function wpautop($pee, $br = 1) {
// Disable WP auto-paragraphing on call
if ( preg_match('/<!--PLAIN_TEXT-->/', $pee) ) return $pee;
$pee = $pee . "\n"; // just to make things a little easier, pad the end
$pee = preg_replace('<br />\s*<br />|', "\n\n", $pee);
// Space things out a little
The rest of the code follows...
Save the file and you're done.
Now, whenever you want to have WordPress treat your blog post as plain text and parse your HTML exactly how you coded it, all you have to do is begin your blog post with this HTML comment line (no spaces and don't forget the underscore ( _ ) between PLAIN and TEXT:
<!--PLAIN_TEXT-->
And boom. Bob's yer uncle. No more wrestling with WordPress trying to get it to behave.
So why not just disable the WordPress auto-paragraphing filter (wpautop) altogether and be done with it? Well this would be great if you're just starting your blog. But if you're like me and you already have a number of blog posts that you wrestled long into the night to get just right, you're probably going to want to leave those just as they are and just disable the auto-paragraphing for any future posts.
And whenever you want, that is.
There may be times when you're feeling lazy or you just don't give a rat's arse what WordPress does or maybe you want auto-paragraphing for a particular post (e.g., you share your blog with your cat who doesn't know much HTML). Who knows? The point is you're free to implement the plain text version at will.
And that's it.
So what do those two lines of code do?
Well, the first line is just a PHP comment line. You don't even need it. Actually, when it comes right down to it, you only need to add the second line of code which uses a regular expression to search for any instance of <!--PLAIN_TEXT--> in your post. If it finds one, it immediately returns the $pee variable (your post content) which aborts running any of the following lines in the wpautop function which, in effect, bypasses the auto-paragraphing filter.
All said and done, the three steps outlined above will create a web developer's version of WordPress as opposed to (how to put this delicately...?) the default Internet for Dummies version of WordPress.
I actually wrote up a plugin to automatically install step #3 but then I backed off and thought—
"What the heck am I doing?!?"
"It's only two lines of code!"
"It would take longer to install the plugin then it would take to insert the two lines of code into the formatting.php file!"
So I scrapped that idea. I figured if people are at the stage where they don't like the way WordPress autoformats their posts then they're up for a bit of two-minute code hacking.
Alright, in case you haven't already noticed uses of plain quotes, character entities and free HTML formatting in this WordPress blog post, here's a demo just for posterity:
"plain double quotes"
'plain single quotes'
Example HTML code:
<img src="http://www.example.com/example-pic.jpg">
Formatted example HTML code:
<img src="http://www.example.com/example-pic.jpg">
Testing...
One line break.
Two line breaks.
Three line breaks.
Hang on a sec. (*sniff*)...
I'm getting a little choked up here with joy. (I think I'm gonna cry...)
Text smiley?
:o)
Oooo... How sweet it is!
Post a comment
| RETURN | TOP | NEXT ~> |
THANKS! gonna try that out. I just got very discouraged by the frigging way wordpress works. Just did it as an exercise, just out of curiosity and then quit!
I feel much more comfortalbe when wrestling with html+css codes.
Very excited about your teaching btw: I bought your ebook (MS DIV) the other day. Working through it, and making changes (banner on top, also a horizontal navibar for MYLINKS).
I haven't found out yet how to make a 1 column-commentslist (CENTERED). But I will get there (soon I hope...)
Okay sir: How about making an ebook and explaining how CMS works? JOOMLA and/or TYPO3. I've been busy with it on and off, but serioussly, I get totally lost, because I just want to use my own html/css coding, and I can’t (yet) find how!
TYPO3 has a stupid RTE (no idea why and I have no clue how to work with it!),
Just used to work with notepad, html-kit and Dreamweaver. I'm only a web-building hobbyist okay, (no design skills), and totally not in your expert-league. Why I'm into this? It keeps me busy and hope it will keep my brain in shape (almost 58). All in all I Just love doing it. TO me, it’s Similar like doing a wooden Chinese puzzle. And whenever I get some code-puzzle worked out, I have this absolutely wonderful YESSSS feeling. (Dunno… Maybe it's an addiction).
Anyway: I need a teacher to make me understand CMS. And I like your way of teaching. Mind you, I'll pay in advance for your (...) CMS-ebook. How about that, huh...?!
Did you also think about video-teaching?
Take care, and keep sharing! Wishes for happiness from Amsterdam/Netherlands
I'm not responding to this particular post, just want to let you know that I discovered your site this morning (11/1/08) and am delighted with it. Your explanations are detailed, complete, clear, and miss nothing. The resources you recommend are excellent. Because I'm a long-time topnotch 8-) technical writer, it's hard to impress me - and I'm impressed.
I got a big laugh out of the results of running validator on some of the major sites. Most of all, though, THANKS.
Nice. Although, I want to play the devil's advocate for the wpautop plugin: when you update, and if the file you edited is changed, there goes your hack. But if it's in a plugin, the hack remains. That's where I'm at with my own hackey plugin.
Thanks a lot for this tip.
I use Dreamweaver to write mys posts, (then I paste the code into the WP html editor) which gives me more control over inserting images at the right place, tables etc.
But Dreamweaver has the annoying habit of creating breaks (empty spaces) in the code, which caused WordPress to insert line breaks. This forced me to go through the code to spot these unintended breaks. Now I don't have to do that any more!
This will save me a lot of time.
I quit using dream weaver ever since adobe made cc2
This is awesome! As someone who also likes to use my own code, WordPress can press on my nerves with some of their auto "corrections". Thank you very much for the help sir!
Hi Rob,
The way you suggest to disable wpautop in WordPress in a desired place of a page or post is just great. I tried several solutions but yours works really fine. Thanks for sharing it.
Great post, ive been thinking of starting my own wordpress blog, as i only have a blogger account, this blog willl come in very usefull.Thanks.
Thank you! This is just what I needed. I was sick of going into a text editor and doing this manually. Awesome post!
I prefer to use wp templates and modify them to meet my needs.
Anyway, this info could be very usefull in some cases.
Thanks for the post. WordPress has been driving me nuts forever with their (apparently) random formatting rules. This helps me quite a bit. I used to just change wp themes thinking that would fix the issue. I switched eligr.com at least 10 times before stumbling across this post.
You have just saved me a lot of head scratching and trying to work this out Rob, thanks very much for that.
Thank for sharing.
Thanx for this.
It works!
You have officially made my day.
Unlike whoever makes decisions about wordpress and its features.
Cheers,
TOPHER.
OH and one more thing in the WYSIWYG you can press shift enter to get a BR tag.
TOPHER.
i honestly cant thank you enought! i have struggled with wordpress for quite a while. death to auto "corrections"!