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.
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:
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.
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 withmyemail@domain.com
and sized to whatever value you specify (between 1px and 512px). This is the function you’d use: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.