scriptygoddess

29 Aug, 2009

Simple Shortcode for Line Breaks

Posted by: Jennifer In: WordPress|WordPress Hacks

One kind of funny thing with WordPress is that entering HTML in the post window can sometimes lead to unexpected results. Sometimes WordPress (or probably more specifically the WYSIWYG visual editor) will eat some or all of the HTML. A friend of mine (Hi Chris!) was asking me how to add line breaks to her post. Adding the actual HTML for a line break didn't work because WordPress ate it. The simplest solution for this is to use shortcodes to get the HTML into the post without WordPress munching it.

To add the shortcode – go to your themes functions.php file (if you don't have this file in your theme, simply create a file called "functions.php" and throw it in your theme folder.) Then add the following code to this file (make sure the function name doesn't class with something else already in there just in case!)

function breakall() {
   return '<br clear="all" />';
}
add_shortcode('br', 'breakall');

Now, when writing your posts, if you need to add a line break or two just add the following where you want the linebreak:


That will be converted to <br clear="all" /> when the page is displayed.

Shortcodes are an amazingly powerful little feature. Read more about shortcodes here:

Shortcode API
Mastering WordPress Shortcodes
10 Incredibly Cool WordPress Shortcodes

1 Response to "Simple Shortcode for Line Breaks"

1 | Shortcodes for WordPress… | Christine ™ aka Big Pink Cookie - Helping Photographers Have Better Blogs & Businesses

August 31st, 2009 at 1:32 pm

Avatar

[…] would share a tip from my friend Jennifer, aka the ScriptyGoddess. She wrote a post recently about using Shortcodes to make line breaks in HTML in WordPress. Definitely something worth checking out if you're in to messing with code behind the scenes […]

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements