Replacing Permalinks With Truly Permanent Links For Everlasting Ranks


There are several usage scenarios when you’d want to replace permalinks with permanent links. Permalink is just another fancy word for your WordPress url structure. However for internal links to be truly permanent you need to use / insert shortlinks instead.

A permanent internal link structure comes handy when:

  1. You change the permalink structure of your WordPress site.
  2. Change the slug of pages (since WordPress doesn’t do an automatic 301 redirect for pages)

The right thing to do is to never insert permalinks at the first place. If you’ve already done that, then here’s the code that replaces them with the shortlinks.

The code is well commented. It certainly can be improved, but it did the job for me. The code works in the loop and needs to be hooked within the loop.

<div>function bt_replace() {</div>
<div>    If ( ! current_user_can('switch_themes')) return;</div>
<div>    $post_id = get_the_id();</div>
<div>    if ( 'page' == get_post_type($post_id) || 'post' == get_post_type($post_id)) { // If we are on a post or a page (we want to avoid attachments etc.)</div>
<div>        $content = get_the_content(); // get the unfiltered content</div>
<div>        // $exp = "/href=[\"|\'](https\:\/\/www\.binaryturf\.com\/[^\"|\']+)/" ; // Uncomment this and set your website url</div>
<div>        $content = preg_replace_callback($exp, 'bt_replace_url', $content); // replacement magic. Match urls begining with our WP site.</div>
<div>        if(is_user_logged_in()){</div>
<div>            wp_update_post( array('ID' => $post_id, 'post_content' => $content) ,$error); // update the post with the new content</div>
<div>            bt_log($error);</div>
<div>        }</div>
<div>    }</div>
<div>function bt_replace_url($matches) {</div>
<div>    // as usual: $matches[0] is the complete match</div>
<div>    // $matches[1] the match for the first subpattern</div>
<div>    // enclosed in '(...)' and so on</div>
<div>    $postid = url_to_postid( matches[1] ); // get the post id from the matched url</div>
<div>    $post_type = get_post_type($postid);</div>
<div>    if ( $postid && ('page' == $post_type || 'post' == $post_type)){ // If this is the link to a post or page (we want to avoid attachments etc.)</div>
<div>        $shorturl = wp_get_shortlink($postid); // convert it into shorturl</div>
<div>        if(empty($shorturl)) {</div>
<div>            return matches[0];</div>
<div>        }</div>
<div>        if('page' == $post_type) {</div>
<div>            $shorturl = str_replace('?p=','?page_id=',$shorturl);</div>
<div>        }</div>
<div>        return str_replace(matches[1], $shorturl, matches[0]);</div>
<div>    }</div>
<div>    return $matches[0];</div>
<div>//add_action('genesis_before_entry','bt_replace'); // uncomment and edit this. any hook within the loop should do</div>
<div>function bt_log($str) {</div>
<div>    echo '<pre>';</div>
<div>    print_r($str);</div>
<div>    echo '</pre>';</div>

Note: After adding this code to your functions.php you will need to revisit all the posts on your blog. You can do this by simply visiting the blog as well.

The catch is, in this particular case wp_update_post is fired on genesis_before_entry. And to update the post content you will have to trigger genesis_before_entry which happens only when the post content is reloaded.

Posted In:

UX UI Front-end Development, Code Snippets

Comment on Replacing Permalinks With Truly Permanent Links For Everlasting Ranks

Your email address will not be published. Required fields are marked *