Warning: Creating default object from empty value in /home/cf9819/public_html/shibumi.net/wp-content/themes/dkret3/header.php on line 17

How To Get WordPress to Link to an External URL

This requires a little code hacking but it’s pretty simple. Open up: wp-includes/classes.php and find function: start_el

After the if else block that sets the value of the $css_class variable add the following code:


$url = get_page_link($page->ID);
$post_url = get_post_meta($page->ID, "url", true);
if ( $post_url ) {
    $url = $post_url;
}

and then remove get_page_link($page->ID) from the following line<a href="' get_page_link($page->ID) . '".... and replace it with $url: <a href="' . $url . '"

Now log in and edit the page or post you wish to specify a customized url for. Add a ‘Custom Field’ with key: ‘url‘ and a value of the full url (including http://) that you wish to send users to. The screenshot below shows the completed hack. NOTE: This will get wiped out next time you upgrade! I’ve also used the same logic to show/hide posts and pages from the menu systems.



4 Responses

Write a Comment»
  1. Tim

    Hi does this actually work an not mess up the whole wordpress cms system… is there not a way that ads in a nice tab or something… as i want to link to an external url in my navs…
    please let me know ill giver it a try any way

  2. Justin

    Tim. It should work and not mess up the whole WordPress system. I’ve used it on a few sites that are strictly using the CMS functionality without any trouble – except for upgrading. To make upgrades easier I recently extracted the whole Walker_Page class to a separate file, commented out the method in classes.php and ‘require_once’ the new file at the top of classes.php. This still requires a little hassle to upgrade but not as much. If anyone else has a better solution please post it.

  3. Tim

    Hey Justin, thanks for for getting back too.. mee.. i know how spam can mess things up…

    i have found a really goo plug in on wp for this… this is only worpress blogs though…

    http://txfx.net/code/wordpress/page-links-to/ took me a while to find it in the massive amounts of google searches i did and wp searches..

    i am so bad at code so this was a simple plug in that worked and ads nice tabs and things into each post or page link.. really usefull…

    http://trcwest.com/blog/

    check out my blog as it has external page links to my main page i bassically am finding all the amazing things that wp can doo… what do you do with cms…

Leave a Comment

Your email is never published nor shared.

(required)
(required)