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.



Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply

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

Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124