Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

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.

share|improve this answer
add comment
up vote 1 down vote accepted

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.

share|improve this answer
    
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. –  m0r7if3r Jan 10 '12 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. –  Evan Mattson Apr 28 '12 at 18:31
add comment

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.

share|improve this answer
    
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." –  Evan Mattson Apr 28 '12 at 1:46
    
Good catch. It's been almost 2 years since I looked at this ... so yeah. –  EAMann Apr 28 '12 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 :) –  Evan Mattson Apr 28 '12 at 18:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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