If you've ever used forum bbcode, then WordPress shortcodes should look very familiar to you. In an earlier chapter recipe, we used the [gallery]
shortcode to specify the number of columns for a post photo gallery. You can add your own custom shortcodes to the functions.php
file of your theme in order to add easy functionality for theme users.
In this recipe, we will create a permalink shortcode so that the theme users can quickly add permalinks to posts that will automatically update if those links change.
First, open up or create a functions.php
file. This is where we will add the permalink shortcode function and register our permalink shortcode.
Next, enter the following code to create the permalink shortcode:
/* Chapter 11 permalink shortcode starts here */ function do_permalink($atts) { extract(shortcode_atts(array( 'id' => 1, 'text' => "" // default value if none supplied ), $atts)); if ($text) { $url = get_permalink($id); return...