Anyone who has worked with WordPress knows how bad the automatic formatting can when you are creating your pages or posts. This will work when the WordPress plugin, PS Disable Auto Formatting, will not.
Thankfully, after hours of testing and looking for helpful advice we stumbled on the best solution; to our amazement it is just a bit of short code.
This short code will disable only the selected portions of text that you do not wish WordPress to auto format, solving this annoying WordPress auto formatting problem!
Place this code within your functions.php file using the standard PHP Layout:
$new_content = ''; $pattern_full =
'{(\[raw\].*?\[/raw\])}is'; $pattern_contents =
'{\[raw\](.*?)\[/raw\]}is'; $pieces =
preg_split($pattern_full, $content,
-1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($pieces as $piece)
{ if (preg_match($pattern_contents,
$piece, $matches))
{ $new_content .= $matches[1]; }
else { $new_content .= wptexturize(wpautop($piece));
}
} return $new_content; } remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize'); add_filter('the_content',
'my_formatter', 99); ?>
Then all you have to do within your WordPress, posts or pages, is block of the sections of text that you do not wish to have the WordPress auto format, using [RAW] tags: [raw]Place the RAW TAGS around the text you do not wish
to have auto formatted, as it is done here![/raw]
Many thanks to TheBinaryPenguin for posting this solution!
Matt: Thanks for this bit of code. The editor was adding break tags and p tags to my shortcodes. It was driving me nuts. Your code does the trick! Thanks!
Kurt, your absolute welcome! This seriously took me forever to figure out for one of my clients when we converted his CSS site to WordPress. We are planning on posting more issues will solutions that we run into as well. If you find anything else that you might be having with WordPress, just let us know and we will try to help out with these issues, as well.
[…] Source: http://ideasandpixels.com/disable-wordpress-auto-formatting-short-code […]
[…] Source: http://ideasandpixels.com/disable-wordpress-auto-formatting-short-code […]
[…] Source: http://ideasandpixels.com/disable-wordpress-auto-formatting-short-code […]
[…] Source: http://ideasandpixels.com/disable-wordpress-auto-formatting-short-code […]
[…] Source: http://ideasandpixels.com/disable-wordpress-auto-formatting-short-code […]
[…] My favorites are related posts , content for Registered Visitors Only and Disable auto-formatting . […]
[…] Source: http://ideasandpixels.com/disable-wordpress-auto-formatting-short-code […]
thank god this worked. nothing else i tried would disable wpautop. Cheers
Me either, Bash!
When I tried this, it created a fatal error at the last } before the functions.. any ideas?
What version of WordPress are you using? I will look into this further with you.
I had same issue. I inserted the code before ?> I removed the last } after which is after
return $new_content; Then, it worked. It could a theme issue. I am using MySiteMyWay Elegance theme.
[…] Source: I&P Blog […]
What is meant by “using the standard PHP layout”?
Standard PHP needs to be placed within carrot brackets
See the image below!
https://www.google.com/search?q=image+basic+php&aq=f&um=1&ie=UTF-8&hl=en&tbm=isch&source=og&sa=N&tab=wi&ei=8isMUZdzsrfSAbG1gYAE&biw=1920&bih=971&sei=VCwMUbGXD4nv0QGr3oDgAQ#imgrc=6JNAVqpuVIdXLM%3A%3BkWMx6h89BKO6FM%3Bhttp%253A%252F%252Fwww.stevedawson.com%252Fimages%252Farticle-images%252F0007-1.gif%3Bhttp%253A%252F%252Fwww.stevedawson.com%252Farticle0007.php%3B395%3B328
content not rendering correctly if content already contains short code …
[raw]Place the RAW TAGS[myshortcoe] around the text you do not wish to have auto formatted[myshortcoe], as it is done here![/raw]
in this above case this function not working as desired
kaNTRi,
Yes I noticed this too. I ended up creating a separate php document (in my case for the contact form) and called it this way. However you cannot use the WordPress short code and have to call on the long format.
I will update this post once I look into this a little more!
Thanks
Help, it’s not working for me. : ( Running WP3.4.1, inserted this code into my child theme’s functions file, but got the white page when I tried to refresh the admin window. My functions file has other code in it. Whether I pasted the code inbetween other functions or at the end, same problem. Left off the ?>, still a white screen. Eliminated spaces, still a white screen. See http://pastebin.com/dz6uDP6V.
Diane,
This code can be stand alone and outside of that function’s php string. Try http://pastebin.com/Wyx0A8Ck
Just paste this at the very bottom and outside of your ?> within your function’s php.
I think it was due to the conflict with the rest of the code in your functions php. This should work and if it doesn’t let me know!
thanks Matt, I got it running and working… Saved me! I was freaking out with thos p tags… just to share with others, I couldnt make the shortcode [raw][/raw] work if I wrote it manually, i had to click on the button on the bar in Text mode…and I renamed the function to whatever i wanted when I pasted the code on my functions.php hope it helps, thanks!
Your welcome! Thanks for the heads up about the tags not working how I currently have it. I will update this post because I wrote it about 3 or 4 WordPress updates ago. Just need to finish our new blog layout first.. sigh (Ha Ha!).
This is probably the issue Diane, below, was having!
Matt thanks a lot. I have a plugin which required inserting a shortcode in a page via Peter’s Include plugin. The WP was driving me nuts for 3 days by inserting and all over on this particular page. I tried old solution of adding remove_filter(‘the_content’, ‘wpautop’); but no help. I also tried WP plugins to disable autop without success. Until, Google search brought me to your page. It worked perfect.
Your welcome Suhaib! This was a major problem for one of our client’s projects and I am glad I was able to find the answer too.
Worked like a charm. Great post. Thanks for sharing.