Posts Tagged ‘shortcode’

WordPress Wednesday: Turn Any Shortcode into a Template Tag

March 3, 2010 18 comments

I previously discussed WordPress’s shortcode API, which allows you to turn any function into a shortcode that you can embed in the content of your pages or posts. Though the example I gave was for a function I wrote, you can use this API for WordPress template tags. The easiest way is to get the Template Tag Shortcodes plugin by Justin Tadlock, although you can also write your own by adding the following line to your functions.php file:

add_shortcode('shortcode_name', 'template_tag');

But what about the other way around? The solution is just as simple with the do_shortcode function.

You may remember that my previous shortcode allowed a client to enter [link pagename=’example’] into the content of any page, and the function would insert the named page’s permalink into the content for him. If I wanted to call that shortcode as a template tag, the tag would look like this:

<?php echo do_shortcode("[link pagename='example']"); ?>

All you have to do to turn any shortcode into a template tag is substitute your shortcode (with the desired parameters defined) between the double quotes (“) and you’re good to go. This is especially helpful when plugins come with shortcodes included but not template tags (like NextGen Gallery).

What will you use the do_shortcode function for?

Update: Thanks to Holling for pointing out that I forgot to include the echo statement. Fixed now.

WordPress Wednesday: Using the Shortcode API to Create Blog Links

December 9, 2009 5 comments

I’m working on a client site using WordPress as the CMS. My client loves how much freedom WordPress gives him to manage his site, but he wants a simple way to create internal links on the site since he is not particularly web-savvy. Obviously, TinyMCE (the default editor) has a button for links, but using it requires copy/pasting the URL and he wants something a little simpler and more dynamic.

The easiest solution is to use the AWS Easy Page Link plugin. Unfortunately, the plugin hard-codes the link, and since I’m building the site on my company’s webspace before pushing the site live I would have to re-code all the links once I move the site to the client’s webspace.

Enter the Shortcode API. Shortcodes allow you to call php functions from within your page or post content using special tags that you define.

First, I create a function in my functions.php file:

//function for creating a link from a page name
function create_pagelink($atts) {

 	//extract page name from the shortcode attributes
 	extract(shortcode_atts(array( 'pagename' => 'home', ), $atts));

 	//convert the page name to a page ID
 	$page = get_page_by_title($pagename);

 	//use page ID to get the permalink for the page
 	$link = get_permalink($page->ID);

 	//create the link and output
 	$pagelink = "<a href=\"".$link."\">".$pagename."</a>";

 	return $pagelink;

Then, directly beneath that function I add the following line:

add_shortcode('link', 'create_pagelink');

Where ‘link’ is the name of the shortcode and ‘create_pagelink’ is the function that I want to associate with the shortcode.

Now if my client wants to create a link (or have me create a link) to a page called “My Example Page” from a blog post or page on his site, we can type the following shortcode:

[link pagename="My Example Page"]

When the blog post or page with the shortcode is visited, the shortcode will be replaced by:

<a href="">My Example Page</a>

Since the function creates the link by first finding the permalink to the page, I don’t have to replace the code when I move the site.