How do I add my own custom shortcodes?

I’m looking to reuse some small portions of html inside several pages/post but I don’t know how to obtain this on WordPress.

It would be nice if I could have a helper in the editor to include them, but this is only a nice to have feature, for the moment I need a solution for this.

Related posts

Leave a Reply

3 comments

  1. Shortcodes work via Shortcode API. Essentially shortcode is just a human-friendly form of writing out data that is processed and passed to associated PHP function.

    So adding your own shortcode involves:

    1. Coding PHP function that would process data, passed by API from shortcode.
    2. Registering that function as shortcode handler.

    I also remember WP Utility Short Codes plugin was recommended in answer to some question while back as good way to insert snippets and such in editor.

  2. So far I found the Shortcoder plugin that allows me to add 20 shortcodes. For the moment this is enough and I hope the developer will solve this limiation soon.

    For some reason this plugin does not appear when I search by its name on WordPress website.

    Also, I’m open to accept new suggestions/alternatives.

  3. I wouldn’t recommend using someone else’s plugin for this. Just build the custom functionality into your theme or add your own plugin (using someone else’s system adds overhead in terms of UI and additional information that you really don’t need).

    Let’s say you want a custom shortcode to add your gravatar image somewhere in the post content. Not sure why you’d need this, but hey … it could be fun!

    Suppose you want to place [user_gravatar user="myemail@domain.com" size="80"] in your posts and pages and have it be dynamically replaced by the gravatar associated with myemail@domain.com and sized to whatever value you specify (between 1px and 512px). This is the function you’d use:

    function user_gravatar_sc($atts) {
        extract(shortcode_atts(array(
            'user' => '',
            'size' => '80'
        ), $atts));
    
        if($user != '') {
            $img = 'http://www.gravatar.com/avatar/' . md5($user) . '?size=' . $size;
        } else {
            $img = 'http://www.gravatar.com/avatar/00000000000000000000000000000000' . '?size=' . $size;
        }
    
        return '<img src="' . $img . '" />';
    }
    
    add_shortcode('user_gravatar', 'user_gravatar_sc');
    

    This function will take an MD5 hash of whatever email address you input and use that hash to request the user’s gravatar image. If you don’t specify an email address, though, it will still return something – the default blue G gravatar placeholder. If you don’t specify a size, it will default to 80px.

    You can drop this code into your theme’s functions.php file or embed it in the body of a custom plugin running on your site. You can see that it’s not very much code, which is why I say using a 3rd party plugin with a complex UI to add the shortcode is adding too much unneeded overhead.