WordPress tips #2 – shortcodes
this is real simple. I’ve give you an example and break it down.
function new_func($atts) {
$str = $atts[word];
// php code here
return //your new string or output here
}
add_shortcode('name', 'new_func');
new_func – this could be named whatever you want
$atts – this is the array variable will store the string you used in the shortcode
[word] this is the array title used, it can be any word you like. You’ll understand more in a moment
add_shortcode('name', 'new_func');
name - is your shortcode name
and as you can see the function name we created is used in here
so here’s what the short code above would look like in use
[name word="blablabla"]
in the above, the string will be stored here: $atts[word] = blablabla
you can add more in this short code
[name word="blablabla" something="hehehehe"]
and then you’d access them in the array with
$atts[word] = blablabla
$atts[something] = hehehehe
A working example
the following is a simple shortcode that I created for another blog of mine that allowed me to easily credit a source and save me a couple steps.
the code is used like this:
[via link="http://www.geekologie.com/2011/03/autonomous_quadrocopters_play.php"]
It takes a link I place in the link=”" and it creates this:
[via geekologie]
and here’s the shortcode
function via_func($atts) {
$str = $atts[link];
$url_array = explode(".", $str);
return '[via '.$url_array[1].']';
}
add_shortcode('via', 'via_func');
more info at WordPress.org
