How to: Point your post permalink to an external site

This was originally posted on ClearSkys.net, but it seems that Barry is going to be shutting down the site. So, that it lives on, I am posting it here. Of note, this is the plugin that I had in mind when I designed that Daring Inspiration WordPress theme.

Any visitors, or subscribers, to John Grubers’ Daring Fireball blog will notice how he uses the post titles as direct links to the sites and information that he refers to within the posts body.

Earlier today WP Recipes posted a quick tutorial on how to set this up on your site (based on work by Vlad Grubman) by adding some code to your themes functions.php file and then changing all the permalink calls within the theme to use the new permalink code instead of the standard WordPress loop function the_permalink().

Whilst this is a perfectly workable solution, it is dependent on modifying your themes functions.php file and all of the pages that include the WordPress loop every time you switch themes (which if you are anything like me, is quite often).

I have modified the code into a small plugin, that when activated, will replace the standard permalink with an external URL without any need to modify your sites theme. I’ve also added an extra filter so that it will replace the permalinks within your RSS feed as well.

The code for the plugin can be found below, and can be downloaded from here: Eternal Permalink. To install, just change the .txt to .php and upload to wp-content/plugins.

`

[php]
<?php
/*
Plugin Name: Enable External Permalinks
Plugin URI: http://jakespurlock.com/2009/09/how-to-point-your-post-permalink-to-an-external-site/
Description: This plugin will allow you to change the post permalink for your posts/pages to an external sites URL
Author: Barry at clearskys.net
Version: 0.1
Author URI: http://blog.clearskys.net/
*/

function cs_external_permalink($permalink) {
global $post;

$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;

$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);

if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey==’url1′ || $pkey==’title_url’ || $pkey==’url_title’ || $pkey==’external’) {
$post_val = get_post_custom_values($pkey);
}
}
if (empty($post_val)) {
$link = $permalink;
} else {
$link = $post_val[0];
}
} else {
$link = $permalink;
}

return $link;

}

add_filter(‘the_permalink’,’cs_external_permalink’);
add_filter(‘the_permalink_rss’,’cs_external_permalink’);

?>
[/php]

`

To switch a post from using the standard permalink to an external one, you simply add a custom field to the post with a key of url1, title_url, url_title or external. The value of the custom field should be the complete URL you want to link to (including http:// ).

27 Comments

  1. Very cool plugin and one I have looked for in the past (which is why this page now resides in my delicious bookmarks =) ). I am assuming this plugin also works for pages and not just posts, right? If so, this is a function that should be considered for the wordpress core! Thanks for your work on this!

  2. yes cool but in my case is only half the answer.
    I have blog with posts from RSS feed–they change every hour or so, but am not getting urls for some reason–your plug in would help with individual posts but am not sure if it would work with rssposts

  3. Hi Jake,
    This is exactly what I was looking for . Im working on a friends site and trying to add this. I have enabled the plugin but cannot work out how to customize a permalink on the post to use an external one.
    can you give an example of a link that was default, and what you entered to make your plugin work?

    We are using simplefolio , and perhaps this is causing complications.

    any help would be fantastic
    thanks!
    Nela

  4. Howdy Jake,

    How would you go about integrating this into the press-this bookmarklet?

    I’d like to be able to publish links to my blog via the bookmarklet and have the URL of the link passed into the custom field via the bookmarklet. Make sense?

    Thanks,

    Kai

    1. I know exactly what you mean, this is something I have wanted for a long time too. Apparently when WordPress 3.1 drops, they have revamped some of the core functions, and have added custom fields. We’ll see soon?

  5. Any quick ideas on how to get this filter to integrate with the post multi-page pagination functionality…? From what I can tell, the tag uses the get_permalink() filter located in wp-includes/link-template.php so my nice “permalink” custom field isn’t used by the pagination shortcode.

    I can hack link-template.php by adding the following at the beginning of the get_permalink function (line 81 in WP 3.0.5) that will act as an intercept for posts who have a ‘permalink’ custom field, but obviously hacking the core is a #1 no-no:


    global $post;

    $thePostID = $post->ID;

    $post_keys = array(); $post_val = array();
    $post_keys = get_post_custom_keys($thePostID);

    if (!empty($post_keys)) {
    foreach ($post_keys as $pkey) {
    if ($pkey=='permalink') {
    $post_val = get_post_custom_values($pkey); } }
    if (!empty($post_val)) {
    $link = $post_val[0];
    return $link; } }

    The basic idea is if a post has a ‘permalink’ custom field then use that. Otherwise, let get_permalink() do it’s thing.

    Any idea where I can put this filter so it will apply to permalinks in paginated posts without having to invade the wp core?

    1. Sorry. The second sentence should read as follows:


      From what I can tell, the tag uses the get_permalink() filter located in wp-includes/link-template.php so my nice “permalink” custom field isn’t used by the pagination shortcode.

      Had an issue posting the tag. :-p

      1. Sheesh. The tag I’m trying to write is:

        Less than, exclamation, dash, dash, ‘nextpage’, dash, dash, greater than

        Don’t know how to get that to show up in a comment. Pretty bad when a person can’t even figure out how to ask their question.

  6. Hi, if though that this code can be simplified. i used add_post_meta named external_link and did it instead to change permalink

    function new_permalink($permalink) {
    global $post;
    $new_permalink = get_post_meta($post->ID, ‘external_link’,true);
    if (empty($new_permalink)) {
    $link = $permalink;
    } else {
    $link = $new_permalink;
    }
    return $link;
    }
    add_filter(‘the_permalink’,’new_permalink’);
    add_filter(‘the_permalink_rss’,’new_permalink’);

    It’s working and it simple
    No i will search to make the link blank

  7. Jake, this is quite wonderful and does nearly everything I need – trouble is, the ‘read more’ at the end of an excerpt isn’t picking it up. See http://www.cualum.org/ – note the feature rotator – the text to the right of the image is the excerpt and it’s -not- getting the new link but the image and the post title ARE. Any ideas?

    1. I worked out a temporary fix, added a class to the excerpt link in functions, then used CSS to hide the excerpt link and wrote a link using Permalink. Ugly but it works.

  8. This is great, but is there a way to add a true permalink to your post in the body somewhere, like Gruber does with the star glyph?

  9. Great plugin! I love it.
    One question: is there a way to put a gliph like a right arrow or something before the linked title? this way people can understand the difference between linked titles and normal titles 🙂

    can you help me?

  10. Jake thanks so much for keeping this plugin alive. You legend.

    Just wondering how I can make this work with custom post types? I’ve used this plugin for many years with standard posts but now that I’m using custom post types I can’t figure out how to make it work for them, too.

    Thanks heaps.

Leave a Reply to Jarad PeckenpaughCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.