Internal Link Shortcode to Prevent Broken Internal Links

To prevent broken internal links in case you change the permalink structure in WordPress, a simple internal link shortcode can be used. Normally it’s best to avoid changing the permalink structure, but if you really have to, internal links in posts or pages would point to non-existent pages.

If instead a shortcode is used each time you link to an internal page or post, a future change of permalink structure would mean that you wouldn’t have to manually change the link URL of each internal link.

Shortcode Function for Internal Links

/*
 * Internal Link Shortcode
 *
 * EX: [internal_link pid="209"]Link text[/internal_link]
 * @param array @atts associative array of attributes
 * @param string @content link text/html content
 * @return string the link
 */
function appglobe_internal_link_shortcode($atts, $content = null) {

   extract(shortcode_atts( array(
      'class' => 'internal-link',
      'pid'  => null
   ), $atts));

   // Return empty string if no post ID provided
   if(!$pid) {
      return ''; 
   }

   $permalink = get_permalink( $pid );
   // Return empty string if no permalink found
   if(!$permalink) {
      return '';
   }

   // Use the page/post title if no content provided
   if( empty($content) ) { 
      $content = get_the_title($pid); 
   }

   return '<a href="' . $permalink . '" class="' . esc_attr($class) . '">' . $content . '</a>';
}
add_shortcode( 'internal_link', 'appglobe_internal_link_shortcode' );

By adding the above code to your wordpress functions.php file an internal link to a post with for example id "209" and the link text "Link text" can then be added like this:

[internal_link pid="209"]Link text[/internal_link]

Creating a plugin for this functionality might be better than putting it in your themes functions.php since it would continue to work even if you change your WordPress theme.

To prevent broken links in case of a future change of permalink structure you would also have to consider external links coming in from other sites, and your old permalink structure URLs already indexed by search engines.
To redirect traffic from those sources (and avoid losing traffic from search engines and referring sites) this permalink structure tool by Yoast could be helpful.

8 Responses

  1. Bob says:

    Can this shortcode be used in a template file?

  2. Jason says:

    I have a website that I integrated a WordPress blog into. I am using a custom theme that is based on the twentyeleven WordPress theme. This custom theme integrates my WordPress blog while retaining the look and feel of my website. The WordPress installation is placed in a folder named “blogspot,” located directly beneath the site root.

    Every page in my main site, and consequently the WordPress blog, has a sidebar section that contains several elements, one of which is a simple php mail-processing form. All this form does is send the data back to me via email. On all of my main pages, when this form is successfully submitted, the form clears, the page reloads, and a lightbox appears informing the user that submission was successful. But on all of my WordPress blog integrated pages, when the form is submitted, or even when it is not submitted due to failed validation but the submit button is clicked, the content section reloads to the 404.php file rather than reloading and re-displaying the exact content showing immediately before the form is submitted.

    My direct blog address is “http://mysite.net/blogspot/.” When the page reloads on form submission, the address shown in the address bar of the reloaded page is “http://mysite.net/blogspot/index.php.” Even when I remove the action attribute from the form tag, successful form submission from any blog page results in a reload of the content section to the 404.php file, although the address in the address bar is correct in this instance: “http://mysite.net/blogspot/.” When I click backspace or the back button, however, I am taken right back to the correct page and content.

    As far as functionality is concerned, the form data is being emailed to me properly and the lightbox is displaying properly upon successful submission from the blog pages. The only issue is that the content section is reloading to the 404.php file instead of reloading and re-displaying the initial content. It’s as if WordPress is mistaking the form submission for an internal search submission, even though I removed and deactivated all of my theme’s widgets.

    So my question is this: How do I get the form and, probably more importantly, the blog pages to behave the way they do when submitted from my non-blog pages? How can I make my blog pages properly reload to the current page when a form integrated via my custom theme is submitted? Would this function prevent the problem I have described from recurring?

    • No, I don’t think the shortcode function could help you with the problem you describe. Also, the link you provided (mysite.net/blogspot/) does a temporarily (302) redirect to emailmyname.com/ … makes it difficult to see exactly what you mean.

      My best guess, though, is that this is a .htaccess / permalink problem.

      • Jason says:

        Oh, no! What a funny misunderstanding! I truly apologize for not being clear enough, but my web address is “http://taxmuscle.net.” The only reasons I used “http://mysite.net” in my original post were 1) because I entered my actual web address in the comment form, and 2) as a simple example.

        I didn’t quite understand why you concluded that my website performed a 302 redirect until I read your reply again and decided to check whether “http://mysite.net” actually exists; and to my surprise, it does! So, I apologize again for the misunderstanding.

        As I stated above, my actual web address is “http://taxmuscle.net,” and the blog within my website is located at “http://taxmuscle.net/blogspot/.” If you go to any of the blogspot-related pages, you will see the behavior I described in my first post, where the content portion of the page reloads to the 404.php file, as if it were interpreting the form submission as an internal search submission.

      • Ok little misunderstanding there :-), Jason.
        About your problem it could be that the action attribute has the wrong URL or – as I previously stated – that this is a .htaccess / permalink problem.
        In any case I still don’t think its related to the internal links shortcode I’m writing about in this article.
        Sorry I can’t be of more help.
        Good Luck!

  3. Saurabh says:

    Hi Martin!
    I am having more than 300 articles on my website and having a lot of internal links with the permalink structure of %postname% all over. Now I wants to connect my blog through google news. The problem is, I can change the permalinks structure but all the interanal links will broked then. Do, I have to update all the internal links with the shortcode you given or just by adding this code all the internal links will automatically got redirected, no need to change?
    Please do reply.

Leave a Reply

Commenting Tips

  • These tags can be used (optional): <a href="" title="" rel=""> <abbr title=""> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strong> <pre>
  • When posting code, please use <code>your code here</code> or <pre><code>your code here</code></pre> for larger blocks.
    Escape < and > with &lt; and &gt;