A WordPress plugin to link pages by ID but still have SEO friendly permalinks
WordPress provides several ways to link to other pages or posts on your blog or web site, but none of them was good enough for what I wanted so I wrote this plug-in.
Requirements:-
-
- Link pages so that even if the page changes its permalink path while you are constructing your site, links from other pages don't break.
-
- Use full SEO friendly permalinks throughout the site, never show a user an url like ?p=145
To use it you find the page or post id (you can see it on any page you are editing in the URL), then add a shortcode like [permalink id=123 text="Link text"]. If you omit the link text it will use the current page title.
With this in place you are free to change any page's permalink during the development process, or even to develop the site under a different domain and then move it to the final domain, and nothing breaks during the process! Of course you shouldn't go changing permalinks after you've deployed your site: that would break inbound links including urls that Google has cached in their search results, but during the development process this plugin can help ensure all the links work even as you make major changes to the structure of your site.
I also added a [childpages id=3] shortcode which gives you a list of pages under a particular page in the navigation structure of your site.
Here's the code, save it to a .php file in your plug-ins directory and activate it.
<?php
/*
Plugin Name: Ian's Page Links extensions
Plugin URI: http://blog.abodit.com/wordpress
Description: Allows you to link to pages using just the page or post ID [permalink id=3 text='xxx'] also provides a way to list child pages under a given page: [childpages id=5]
Version: 1.0
Author: Ian Mercer
Author URI: http://blog.abodit.com
License: GPL2
*/
function ian\_childpages(\$atts) { extract(shortcode\_atts(array( 'id' =\> 8 ), \$atts)); \$children = wp\_list\_pages('title\_li=&child\_of='.\$id.'&echo=0'); return '\<ul\>'.\$children.'\</ul\>'; }
function ian\_permalink(\$atts) { extract(shortcode\_atts(array( 'id' =\> 3, 'text' =\> 'Missing' ), \$atts)); if (\$text == 'Missing') \$text=get\_the\_title(\$id); return '\<a href="'.get\_permalink(\$id).'"\>'.\$text.'\</a\>'; }
add\_shortcode('childpages','ian\_childpages');
add\_shortcode('permalink','ian\_permalink');
?\>