An Alternative to the Shortcode Madness [Part 1]

An alternative to the shortcode madness [Part 1]

I tend to spend a good amount of my time continuing to learn WordPress and improve the ways I go about coding themes. With the vast majority of my WordPress development being done for clients, it is important to me to keep a good relationship with them. I feel part of maintaining that relationship is to provide a seamless, simple, and easy solution to their website project. I refer to this as the “Don’t make me think” approach, which pairs nicely with “Just because you can, doesn’t mean you should”…

In most cases, clients spend most of their time adding/editing content in the editor. It is then important to keep things simple yet provide as much functionality as possible. If your clients or users are anything like mine, more than likely they don’t spend the vast majority of their time cleaning code in their favorite text editor and are probably not catching up on their RSS feed full of their favorite web related sites just before they go to bed at 2 am like the rest of us. Clients also get confused when they hear “It’s as simple as adding a class to the element” when trying to create the desired green glossy web 2.0 button. Usually this will lead you to a variety of blank stares as their idea of a class and elements is a room of kids and the periodic table.

Basically, for me to expect my clients to know, remember, and filter through the HTML to add a class to an anchor tag, buried in the middle of the text, is not my idea of a seamless, simple, and easy solution. This goes without mentioning the sometimes bizarre and crazy markup that can be made by the visual editor in the first place (insert previous “cleaning code” remark here ____).

So then, how can the client or user make that link look pretty and green, with rounded corners, a text shadow… and the groovy gradient gloss effect?

SHORTCODES!!!or simply select the style from a drop down menu within the visual editor.

Implementing A Custom Styles Drop Down Menu

The first alternative up for debate is to provide a custom styles drop down menu. This method is simple to implement for developers and simple to use for users. This is by no means revolutionary as it has been done before, but it is certainly effective. Part of the beauty of adding a new styles drop down into the editor is:

  1. The client does not have to remember a class name, or a shortcode name for that matter… both of which are usually found written down on a yellow post-it-note stuck to the outside edge of their monitor. I have an alternative for this as well that will be covered in another article.
  2. It’s inside the visual editor. No need for the HTML view and code sifting.
  3. It’s a common practice and recognized functionality.
  4. “Don’t make me think” rule applied.

Below is the code necessary to implement a simple styles drop down box.

/** 
 * Filter TinyMCE Buttons
 *
 * Here we are filtering the TinyMCE buttons and adding a button
 * to it. In this case, we are looking to add a style select 
 * box (button) which is referenced as "styleselect". In this 
 * example we are looking to add the select box to the second
 * row of the visual editor, as defined by the number 2 in the
 * mce_buttons_2 hook.
 */
function themeit_mce_buttons_2( $buttons ) {
  array_unshift( $buttons, 'styleselect' );
  return $buttons;
}
add_filter( 'mce_buttons_2', 'themeit_mce_buttons_2' );

/**
 * Add Style Options
 *
 * First we provide available formats for the style format drop down.
 * This should contain a comma separated list of formats that 
 * will be available in the format drop down list.
 *
 * Next, we provide our style options by adding them to an array.
 * Each option requires at least a "title" value. If only a "title"
 * is provided, that title will be used as a divider heading in the
 * styles drop down. This is useful for creating "groups" of options.
 *
 * After the title, we set what type of element it is and how it should
 * be displayed. We can then provide class and style attributes for that
 * element. The example below shows a variety of options.
 *
 * Lastly, we encode the array for use by TinyMCE editor
 *
 * {@link http://tinymce.moxiecode.com/examples/example_24.php }
 */
function themeit_tiny_mce_before_init( $settings ) {
  $settings['theme_advanced_blockformats'] = 'p,a,div,span,h1,h2,h3,h4,h5,h6,tr,';

  $style_formats = array(
      array( 'title' => 'Button',         'inline' => 'span',  'classes' => 'button' ),
      array( 'title' => 'Green Button',   'inline' => 'span',  'classes' => 'button button-green' ),
      array( 'title' => 'Rounded Button', 'inline' => 'span',  'classes' => 'button button-rounded' ),
      
      array( 'title' => 'Other Options' ),
      array( 'title' => '½ Col.',      'block'    => 'div',  'classes' => 'one-half' ),
      array( 'title' => '½ Col. Last', 'block'    => 'div',  'classes' => 'one-half last' ),
      array( 'title' => 'Callout Box',        'block'    => 'div',  'classes' => 'callout-box' ),
      array( 'title' => 'Highlight',          'inline'   => 'span', 'classes' => 'highlight' )
  );

  $settings['style_formats'] = json_encode( $style_formats );
  return $settings;
}
add_filter( 'tiny_mce_before_init', 'themeit_tiny_mce_before_init' );

Now, to apply our green button style to a link, all the user has to do is highlight the links text and select the “Green Button” style from the new drop-down. This link will now take on the simple and universal CSS-based button style set in your themes stylesheet. There is various methods to doing this. Instead of a span tag for the button, you could use an “a” tag. Play around and see what you think.

Take It Another Step Further with Editor Styles

So now we have made things easier for our users to style elements without having to touch any code or add a single shortcode. Let’s take things one step further and add those styles to the visual editor so the users can see the results. To do this, we will simply add a few lines of code to the functions.php and create a new stylesheet for the editor to use.

/**
 * Add Editor Style
 *
 * This provides the theme with the functionality to add a custom
 * TinyMCE editor stylesheet. By default, the add_editor_style() will
 * look for a stylesheet named editor-style.css in your theme. Here
 * we have chosen to define our own stylesheet name, style-editor.css.
 * This stylesheet can be named whatever you want, just be sure it is
 * defined in the function below and included in your theme files.
 *
 *{@link http://codex.wordpress.org/Function_Reference/add_editor_style }
 */
 
function themeit_add_editor_style() {
  add_editor_style( 'style-editor.css' );
}
add_action( 'after_setup_theme', 'themeit_add_editor_style' );

By providing the add_editor_style() function, we are telling WordPress the theme is including a custom TinyMCE editor stylesheet. Now we can include any custom styles the theme may use into a css file called style-editor.css, which we defined in the add_editor_style() function. By default, if a stylesheet name is not defined here, WordPress will look for a file called editor-style.css. I usually change the name to style-editor.css for organizational purposes so the file is listed next to my style.css file. For a great starting point, take look at the Twenty Eleven theme’s editor-style.css file.

Down the Road…

I have a few other, possibly more advanced methods, I would like to share down the road regarding this topic of shortcode alternatives. These methods are a little more robust and allow for more advanced styling to elements. For the sake of an already lengthy article, I’ve decided to split it up into multiple “Parts”. In short, these methods include the use of Quicktags, Advanced TinyMCE Buttons, and “Smart” Editor styles. If your like me and prefer to dig around in code to figure things out on your own, all these methods are currently used in the Debut Theme on Press75.com.

I'm the owner and operator of CELTIC7 Studio, a proud member of 1% For The Planet, a theme author on Press75.com and ThemeGarden.com, and an avid music collector. I can be found on Twitter @thelukemcdonald.

60 Comments on "An Alternative to the Shortcode Madness [Part 1]"

  1. Jake says:

    This is awesome, Luke. I’ll definitely throw this into my next theme. Thanks!

  2. Paul says:

    Never thought of this until now. Thanks for the post.

  3. Super useful, thanks for writing this. Can’t wait to see the other parts

  4. Evan Mullins says:

    Great ideas! Although you say they aren’t new ideas, I hadn’t put 2 & 2 together and actually done this for my clients. Another idea, which would be more complicated would be to add a preview pane to show the styles applied, maybe in conjunction with the new full screen editor…
    Thanks again! I’ll be watching for the other “parts” =)

    • Thanks, Evan! You can show the styles applied by adding them to the custom editor stylesheet mentioned towards the end of the post. If a class has been applied to an element via the drop-down, those styles will be reflected in the editor. Hope that makes sense?

  5. John Morris says:

    Nice! Good to see somebody coming up with a solution that works for everybody. Really cool that you posted the code, as well.

  6. slhomme says:

    Great post, smart solution, thanks for sharing! Looking forward to part 2.

  7. Jeremy says:

    Great idea, and great post, thank you Luke! I can’t wait for part 2!

  8. Jenni says:

    Very nice idea, I like the way that you add the dropdown menu into the text editor form, I don’t know how to do this before.
    Thank you!

  9. Tom Hermans says:

    Looks a much “nicer” way to have for clients.. but this still renders useless when someone changes theme, right.. (although I get why..) cause that’s one of the major things I don’t like about shortcodes..

    • Not necessarily. The point of this is when you switch themes, you won’t have the shortcode showing, for example, [one_half]Your content in a half column[/one_half]. In this case, you would have to go to every single page and post that utilized a shortcode, not supported by the current theme, and remove it. In this example, although very basic, the html elements will still be in place, but NOT visible.

      There is another advantage to this method over shortcodes. With a shortcode, either the theme must support it or you have to go in and add a bunch of extra coding to create the it. Using this method, because the elements, classes, and ID’s are still there, you can simply create some css styles to style those elements.

      Shortcodes definitely are useful and have their place. Styling elements and using them for page layout probably isn’t the best use.

      • James says:

        Hi Luke,

        Your last comment is very interesting to me. When exactly would be the best time to use a Shortcode over this method you’ve blogged about? I’m currently developing a WordPress site for a client and I want them to have to use as few shortcodes as possible. So this Style dropdown will be great.

        However, when exactly do you think that Shortcodes should be used instead?

  10. Colin says:

    I wanted to visually style the added styles in the editor and came across this little tutorial. Pretty simple to implement.

    http://azaozz.wordpress.com/2010/01/02/can-themes-style-the-visual-editor/ to stylize

  11. Gene says:

    This is great! Using it in my first theme, however I have a question. Say I’m using the 1/2 Col div as a shortcode as shown above in the post, but I would also like that div to contain paragraph tags. How would that be done?

    i.e: instead of the out being “div.col-2″, I want it to be “div.col-2 >> p”? (as shown in the visual editor “path”).

  12. JohnB says:

    @Gene, you’ll want to make those divs wrappers. This way, they hold other block-level elements properly, e.g. p elements like you mentioned, or nested divs. To do that, add a wrapper attribute to the style declaration like this:
    array(‘title’ => ‘Column’, ‘block’ => ‘div’, ‘wrapper’ => 1, ‘classes’ => ‘column’)

  13. Lee Mason says:

    Thanks for this, it is what i have been looking for for a long time.
    And strangley it is easier than adding shortcodes for many functions.

    Ive implemented a splitbutton with a drop down list for all my “them shortcodes” (note not shortcodes anymore, just html formatting).

    The one problem i found was when using divs that clear content (empty clearfix divs).

    These where getting stripped out when flicking between the tinymce and html editor (and on save).

    I FOUND A SOLUTION!!!

    a slight alteration to your code above for the function which filters the tiny_mce_before_init

    before returning the settings add this snippet:

    $settings['verify_html'] = false;

    This will prevent the editor stripping out your tags.

    Unfortunatley this will prevent all html validation, i havent came across an issue with this, but others may so try it out and see what happens.

    Ive tried reading the tinymce documentation and it seems a more specific approach might be to configure “valid_elements” with the clear div added to this array. I dont know how to do this, but maybe someone here does.

  14. Amy says:

    Brilliant. As a WordPress trainer I often end up with very confused users who say “I’ll just have you do it” instead of trying to learn or figure it out themselves. So many themes have non-standard “features” it makes it very difficult from an end-user perspective. I think this is at least a step in the right direction. Thanks, Luke.

  15. Lee Mason says:

    Another thing i would suggest with this is still to provide the shortcode, even if its not as good.

    most shortcodes for layout etc have established a naming structure which works with many different themes.

    Its alot to ask a theme user to redo all of there content, offering both gives the user a better choice

  16. Thanks for the great tutorial! I keep meaning to dig more into customizing Tiny MCE, and tutorials like this are very helpful. I’m running into some odd behavior with the formats dropdown when I include this while testing it out. The theme_advanced_blockformats setting changes cause several blank lines in the format options (for the blocks it doesn’t recognize), and for options that are left out, like pre, address, etc. Any thoughts on how to add block elements without breaking up the standard formats menu?

  17. Great article. Now for a “dumb” question: do the two blocks of code both go into the theme (or child theme’s) functions.php?

  18. johnny says:

    Thanks! This is a fantastic and clean way of adding some extra (and much needed for our clients) functionality to the wordpress editor.

  19. msrosyidi says:

    If I find this article one year ago…I’ll use it. Thanks, this is a article..

Trackbacks for This Post

  1. Dispatch WordPress Theme » A Few Custom Styles
  2. 20 Useful Shortcodes You Can Use In Your Next Project | Wptuts+
  3. CMYKrack» Blog Archive » Resource Roundup! 20 Creative Shortcodes To Use In Your Projects
  4. A Free wordpress newsletter » 20 Really Handy Tutorials to help you build WordPress Themes
  5. 20 Really Handy Tutorials to help you build WordPress Themes | Customize WordPress Blog
  6. Building WordPress Themes You Can Sell
  7. Building WordPress Themes You Can Sell@smashing | seo博客大全
  8. Building WordPress Themes You Can Sell | IdentityNepal.com
  9. Building WordPress Themes You Can Sell | Appenheimer
  10. Building WordPress Themes You Can Sell | Pinoy Templates
  11. Building WordPress Themes You Can Sell | DesignYourCss
  12. Building WordPress Themes You Can Sell | Ricky Noel Diancin Jr. Webmaster | Web Designer | Wordpress Expert
  13. Building WordPress Themes You Can Sell | Blogs – NG Outsourcing
  14. Building WordPress Themes You Can Sell | Ruturaj Pradeep Kohok | Your Web Advisor
  15. Building WordPress Themes You Can Sell | 陈华伟的WordPress
  16. An Alternative to the Shortcode Madness [Part 1] | | Mark Wilkinson on WordPress Mark Wilkinson on WordPress
  17. Building WordPress Themes You Can Sell | Remake Wordpress Theme
  18. Building WordPress Themes You Can Sell | Testing themes
  19. Building WordPress Themes You Can Sell | Wordpress Training Course Brisbane: Next Course Thur 24th Nov 2011
  20. Premium plugins | Building WordPress Themes You Can Sell
  21. Building WordPress Themes You Can Sell | Web Design Course Brisbane: Next Course Sat 10th Dec 2011
  22. level. graphic design boutique agency based in weymouth dorset
  23. Building WordPress Themes You Can Sell | underwaterfashionblog.com
  24. Building WordPress Themes You Can Sell | Suryakanthi
  25. » Building WordPress Themes You Can Sell DESIGNLANDER
  26. json Encoding Information Resources | Q&A System
  27. Are shortcodes the best solution for your WordPress theme? | OCHOLABS
  28. Download Zipfolio – Single Page Portfolio WordPress Theme free – Daily Sharing Wordpress, Joomla, Magento free premium Template | Daily Download Wordpress, Joomla, Magento Templates
  29. Zipfolio – Single Page Portfolio WordPress Theme (Portfolio) | Wordpress
  30. Zipfolio – Single Page Portfolio WordPress Theme (Portfolio) ‹ Premium CSS, WORDPRESS, Buddypress, PSD, SITE themes
  31. Amazing Template Showcase – Zipfolio – Single Page Portfolio WordPress Theme (Portfolio)
  32. Zipfolio – Single Page Portfolio WordPress Theme (Portfolio) | Themes Tub
  33. Zipfolio | WPbase
  34. Zipfolio wordpress theme download. HotFile & Filesonic.
  35. Zipfolio – Single Page Portfolio WordPress Theme | Dinopress – Internet Wordpress Themes Directory

Got something to say? Go for it!