WordPress Internal Permalinks by ID Shortcode

Adding internal links to pages or posts in WordPress can be a lesson in frustration.

WordPress permalinks functions let you pick the page title (slug) as the link, to optimize for search engines (by modifying the slug) and move posts and pages to being children of others. All of the above changes the permalink. To keep from having to update your internal links as the permalink changes requires a little WordPress mumbo-jumbo.

Plugins

There are a few WordPress internal link plugins to choose from that will do the job, but I didn’t find one that suited my purposes. I wanted to be able to modify the link text and title for the link for SEO, but most of these plugins wouldn’t let me. So I came up with my own shortcode to do the job.

Theme Function for Permlink by ID shortcode

Add this code to the end of functions.php (Theme Functions):

function perm_link($atts) {
$fpid = get_option('page_on_front');
extract( shortcode_atts( array(
'id' => $fpid,
'text' => 'linktext',
'title' => '',
), $atts ) );
if(empty ($title)){$title = $text;}
$link = get_permalink($id);
$permlink="<a href="".$link."" title="".$title."">".$text."</a>";
return $permlink;
}
add_shortcode('perm-link','perm_link');

The Shortcode

The shortcode works as follows:

[perm-link text=’link text’ title=’link title for hover’ id=pageid]

  • ‘link text’ is what you’ll see for the link
  • ‘title’ is what you’ll see when you hover over the link
  • ‘id’ is the page id of the page you’re linking to

Finding the Page IDs

To find the id of any page or post, hover over an edit link on the page or post, or in the dashboard, and the id will show in the status bar at the bottom of your browser window. There are also plugins that will show the page ids in the admin dashboard.

This code renders the complete URL (not a relative link). It automatically updates if a page or post is moved to child status (or to root), is moved to another category or the page permalink (slug) is manually changed in the dashboard. It draws the links from the WordPress permalink structure so has no conflicts.

The shortcode works in the wysiwyg or html editor and if shortcodes are enabled in widgets, works there too.

Once an internal link is set using this code, it’s set for good. XHTML valid

Update:

As of WordPress 3.0.1, there is now an internal linking utility built in. Links built using this shortcode will continue to work, but you can make internal links easier using the built in link dialog.

View Comments

2 Responses to WordPress Internal Permalinks by ID Shortcode

  • JonB

    Hi –

    Looks very eeenteresting! I’m going to try it out. (and if adopted — of course — give credit)

    Thanks for your efforts and sharing them.

    JonB – the server geek

  • MuneyMan

    Greetings,
    Just wanted to leave a comment and thank you, you’ve made implementation into the functions very easy and unlike most sites you did not include the code in an image forcing those unfamiliar with code to guess at spaces and special characters trying to duplicate the code on their own.

    Appreciate it!
    A+
    -J

Leave a Reply