Last Updated on April 3, 2020 by Neil Murray
This site uses many internal links to relate to other pages. It is very easy to create a post/page link to other page using WordPress editor, but once we modify the page slug, move the page to other hierarchy, or move the site completely with different domain, we will got 404 not found page.
For a workaround for this issue, we should avoid static link to a post or page, and use a shortcode that generates the link automatically based on post ID. We can find the ID of post or page in respected admin edit screen as screenshot above from https://team.cf7skins.com/wp-admin/edit.php?post_type=page for Page type.

Creating Shortcode Link
Creating a shortcode in WordPress is easy, we just need to register our shortcode function using add_shortcode and shortcode_atts to combind user shortcode attributes with default shortcode attributes.
Here is how the code looks like:
function cf7skins_link( $atts ) {
$atts = shortcode_atts( array(
'id' => 701,
'text' => '',
), $atts, 'link' );
extract( $atts, EXTR_SKIP ); // extract attributes
if ( $permalink = get_permalink( $id ) ) { // return string if found or false.
$title = $text ? $text : get_the_title( $id );
return "<a href='$permalink'>$title</a>";
} else {
return 'Error, the post or page does not exist!';
}
}
add_shortcode( 'link', 'cf7skins_link' );
The shortcode takes two attributes or parameters, id for the post ID and title for custom post/page title. If title is not set, than it will use the post title itself. The function will finds the post ID, create the link if found, or display error message if not exist.
To use the shortcode, simply add a square bracket with link string, provide the post ID and custom title if needed.
[link id="144"]
That’s it, with a few lines, we can add post/page link automatically, and save the site from 404 suffering.
Hi Sastra, thanks for posting about this. I first replied saying I didn’t quite understand how to actually implement this and how to do it. Regardless of not understanding your instructions, I think I understand the concept and have given it some thought. I think we should not be changing the slugs if it can be avoided and keeping the the original URLs for all links.
The main reason for this is the blog structure & URL structure of permalinks is very important for SEO. We need to maintain these links and continue to build our library in a way that can be read by search engines. Using an ID # is not helpful for Google, the links will not appear in searches, and it will also be very difficult to understand Google Analytics reports without the slug.
For some reading on this, take a look at the following:
What do you think? Curious to hear everyone’s thoughts.
Hi Katie.
Actually it is not implemented yet in tnis team.cf7skins.com site, but rather like a tutorial on how to add internal link automatically.
You’re right about the permalink for SEO. The example code above uses site setting to generate the permalink. Blog post in this site is not exposed to public, so we don’t need to worry about SEO. And as your point above, the URL structure is also important to help us understand each page.
This is not an issue on our team sites which are not exposed publicly.
Note: We request that Google does not crawl this site.
Ahh, he meant for internal posts. Got it.
+1 – we change the structure of this site frequently for very good reasons.
Sastra’s post offers a way for us to cope more easily with frequent URL changes on this website.
@Katie – I’ve added your info on SEO at https://team.cf7skins.com/documentation-guidelines/our-seo-standards/