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.

3 Answers 3


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.


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.

  • Depending on how basic the code you're trying to reuse is, the TinyMCE styles dropdown may be a better route to take than shortcodes. Adding shortcodes is incredibly easy, if you provide an example (in the OP) of what you're trying to do, most people here wouldn't mind giving specific implementation advice.
    – mor7ifer
    Commented Jan 10, 2012 at 22:08
  • I would also like to recommend this plugin (Shortcoder) as one of the best that I have found for creating custom shortcodes. Very simple & easy to use UI and .. everything works. The other plugin I would recommend for more advanced shortcodes would be Post Snippets. Commented Apr 28, 2012 at 18:31

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="[email protected]" size="80"] in your posts and pages and have it be dynamically replaced by the gravatar associated with [email protected] and sized to whatever value you specify (between 1px and 512px). This is the function you'd use:

function user_gravatar_sc($atts) {
        '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.

  • I'm sure you knew this but it's important to note that the function should return the value instead of echoing it. From the shortcode api codex page: "The return value of a shortcode handler function is inserted into the post content output in place of the shortcode macro. Remember to use return and not echo - anything that is echoed will be output to the browser, but it won't appear in the correct place on the page." Commented Apr 28, 2012 at 1:46
  • Good catch. It's been almost 2 years since I looked at this ... so yeah.
    – EAMann
    Commented Apr 28, 2012 at 3:25
  • haha no big deal. I've only just been active on this forum for a few months now and I have already seen your posts all over the place so I know you know what you're doing. I just wanted to point that out for other n00bs like me :) Commented Apr 28, 2012 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.