Home > Web Design, WordPress > WordPress Wednesday: Using the Shortcode API to Create Blog Links

WordPress Wednesday: Using the Shortcode API to Create Blog Links

December 9, 2009

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="http://www.clientsite.com/my-example-page/">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.



Advertisements
  1. January 9, 2010 at 10:56 AM

    great idea. I’ll be using this trick for sure.

    Would be cool to let is call links from the WP links (bookmarks) as well…

    • Michael Dozark
      January 15, 2010 at 8:49 PM

      That would be cool. I’ll have to look into that.

  2. March 26, 2010 at 1:58 PM

    This is brilliant! Happy I stumbled on this after a quick search for “wordpress shortcode for page id”. Dynamic generation of links within a WordPress site has been one of its shortcomings when used as a CMS, but this is a great solution! It would be great if this could be incorporated into a plugin that ties into the TinyMCE editor.

  1. January 28, 2010 at 2:34 PM
  2. March 3, 2010 at 9:53 AM
Comments are closed.
%d bloggers like this: