The Shortcode API
Creating shortcodes in WordPress is a matter of creating the function that prints out what you want, and then defining it as a shortcode. In the case of Next Page, the shortcodes are just alternatives to the template tags—they do the same thing. Since that's the case, and there are no arguments to be dealt with, creating the shortcodes takes all of three lines, as shown in Listing 9-17.
Listing 9-17. Creating the three Next Page shortcodes add_shortcode('previous', 'previous_link'); add_shortcode('next', 'next_link'); add_shortcode('parent', 'parent_link');
That's it! Add these lines to the end of the Next Page plugin file, and you can now use [previous] in the post or page content as an alternative to using previous_link() in a theme file.
Of course, shortcodes can be more complicated. They can accept arguments, and you can have opening and closing shortcodes that surround a bit of text. Since Next Page doesn't demonstrate all that, I'll show you the shortcode processing function from another plugin of mine, Simple Internal Links, which lets you easily link to content within your WordPress site. This plugin provides the shortcodes shown in Listing 9-18. The corresponding plugin function is shown in Listing 9-19.
Listing 9-18. The Simple Internal Links shortcodes
// creates a link to post/page ID 6180 and uses its title as the linked text [link id="6180"]
// uses the given text instead of the post/page title [link id="6180"]link text[/link]
// links to post/page using the slug instead of the ID
[link name="post-slug"]
[link name="post-slug"]link text[/link]
// links to post/page using the title
[link title="About Us"]
[link title="About Us"]link text[/link]
// links to a category by slug
[link cat="cat-slug"]
[link cat="cat-slug"]link text[/link]
[link tag="tag-slug"]
[link tag="tag-slug"]link text[/link]
Listing 9-19. Simple Internal Links shortcodes function create_anylink($atts, $content = null) {
//extract page id from the shortcode attributes extract(shortcode_atts(array( 'id' => 0, 'name' => null, 'cat' => null, 'tag' => null ), $atts));
$catobj = get_category_by_slug($cat);
$link = get_category_link($catobj->term_id);
if (empty($content)) $content = get_cat_name($catobj->term_id);
$tagobj = get_term_by( 'slug', $tag, 'post_tag' ); $link = get_tag_link($tagobj->term_id); if (empty($content)) $content = $tagobj->name;
$thepost = get_page_by_title($title);
if (!$thepost) { // there is no get_post_by_title, so we'll sanitize and try it as a slug global $wpdb;
"SELECT ID FROM $wpdb->posts WHERE post_name = %s AND (post_type = 'post' OR post_type = 'page') ", sanitize_title($title) )); $thepost = get_post($id);
$thepost = get_post( $id ); // will get all post types $content = $thepost->post_title;
// if no ID, get post/page by name or title if (empty($id)) { global $wpdb;
"SELECT ID FROM $wpdb->posts WHERE post_name = %s AND
(post_type = 'post' OR post_type = 'page') ", $name )); }
// use $content for the title if it exists; otherwise fetch page/post title if (empty($content)) {
$thepost = get_post( $id ); // will get all post types $content = $thepost->post_title;
return '<a href="'.$link.'">'.$content.'</a>';
else return $content; // no cat, ID, or name provided; ignore this shortcode
add_shortcode('link', 'create_anylink');
The create_anylink() function takes two arguments: the attributes of the shortcode and the content enclosed by the shortcode. Since some shortcodes, like [gallery] , stand alone, you can assume that $content is empty. The extract function merges the array of default values and the array that was passed and extracts them into named variables. Then you see which argument you got. For each possible argument, you see whether a title was provided, and if not, you fill in the link text with the post/page/category/tag title.
The method of finding the permalink varies depending on which arguments were used. Most notably, there is no get_post_by_title() function. Instead, I've sanitized the given title using the same function that creates automatic permalink slugs on the Edit screens. Unless the user has changed the post slug manually, this should give you a slug that you can use to find the post ID.
For more information on shortcodes, see the Codex page at codex.wordpress.org/Shortcode_API.
Post a comment