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:

[br]

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

Related posts:

  1. Adding a block of HTML to a WordPress post One problem with the WYSIWYG editor in WordPress is that...
  2. Anchor Links in WordPress Posts – another shortcode solution I was recently asked by a client how they could...
  3. Conditionally change path to HTTPS One of my clients had set it up so that...
  4. Striping IMG tags from the_content in WordPress (and how to fudge page excerpts) Background: For a site I was working on, I was...
  5. Multiple Featured Content Galleries On a site I was working on recently, the client...

Related posts brought to you by Yet Another Related Posts Plugin.

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 [...]

Comment Form

Featured Sponsors


  • Michael: You can use get_header(2) in your case. The filename of your custom header has to be header-2.php. The problem with include(your_file.php) is, all
  • cliff: hi wonder if you can help me, pls i designed www.kouga.mobi using dreamweaver CS3 and now want to make the phonenumbers into links so that if you
  • jerey: how do i rewrite this because it tried RewriteEngine on #Options +FollowSymlinks RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FIL

About


Advertisements