WordPress Geekery: add shortcodes for your clients

This scenario works perfectly if:

1 – some part of the post that should be hidden from printers for copyright reasons,

2 – or if you need a certain section styled differently

3 – AND your client doesn’t want/need to use the HTML editor.

Warning – for Developers only!

The following is code that should be inserted into the functions file only by your website developer.  ANY mistake at all (like a missing “;” ) will break the entire site!

This goes without saying, but I’ll say it anyway: never ever ever edit a site live.

Goal: Simple Shortcode that adds div classes

This code will add the shortcodes, but not the buttons to the TinyMCE editor. You’ll have to train your clients to memorize and/or use them.

In this case, our client wants the section of the post about a ‘story’ styled differently. We’re going to add a shortcode that simply assigns a “story” class to this section.

Shortcode Functions in WordPress (Also Genesis Framework)

add_shortcode('story', 'dswp_storydiv');
add_shortcode('end-story', 'dswp_endstorydiv'); 

/* Open Div */ 
function dswp_storydiv($atts) {
extract(shortcode_atts(array('class' => 'story', 'id' => '' ), $atts));
 $return = '<div';
 if (!empty($class)) $return .= ' class="'.$class.'"';
 if (!empty($id)) $return .= ' id="'.$id.'"';
 $return .= '>';
 return $return;
}

/* Close Div */
function dswp_endstorydiv($atts) {
        return '</div>';
}

References:

http://codex.wordpress.org/Function_Reference/add_shortcode

Share to Facebook Share to Twitter Share Share to StumbleUpon