BLOG

See what we're whipping up!

I’m working on a site currently and thought I would share a code snippet I created that has been awesome for me! If you didn’t already know, I’m a huge fan of using Advanced Custom Fields. Sure, I could code in something myself, but it just makes it so much easier to use ACF. The drag-n-drop fields, the conditional logic; what’s not to love? For those without a ton of knowledge about WordPress, it makes the interface that much easier.

Anyway, the site I’m working on often posts links to other sites within the loop using custom post types. I could, very easily using Advanced Custom Fields, make a URL field and insert it into the loop around the title of the article, instead of having the permalink to the post itself in place. The problem is that if you use that method, the posts are still being generated, which can be a nightmare for SEO. Having hundreds of blank posts isn’t exactly fantastic. So, I created this function that takes the URL field and makes that the permalink for the post created. That’s right! No blank pages!

So, how would one do this? First off, if you haven’t yet, read my previous post, because I outlined the best way to upload custom functions to a site. If you’re already aware of how to do it, basically you make a plugin for the site itself. Functions can be coded there with no problem and can be easily deactivated if any issues arise. On to the code itself!

// START External Permalink - Links
add_filter( 'post_link', 'wpdevelopers_permalink_links', 10, 2 );
function wpdevelopers_permalink_links( $link, $post )
{
$meta = get_post_meta( $post->ID, 'link_url', TRUE );
$url = esc_url( filter_var( $meta, FILTER_VALIDATE_URL ) );
return $url ? $url : $link;
}
// END External Permalink - Links

Here’s what can be edited:

wpdevelopers_permalink_links

This is the function name and it can be changed to whatever name you’d like. Just make sure it makes sense and make sure both instances of it in the code match!

'link_url'

This is where you place your custom field name. You can easily get the field name by looking at your field group within Advanced Custom Fields, and then look under “Field Name”. Don’t get the field name confused with the field label!

And viola! That’s it! Now, if you enter a URL into the field, the post’s permalink will now be to that custom URL. If you enjoyed this, go ahead and follow us on Facebook and Twitter!

UPDATE

A good question is what if I want the external link, within the loop, to open in a new window? Well, we can accomplish this by using a conditional statement within the loop. For example, you probably have something within your loop that looks like this:

<a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a>

This, obviously, will output your post title, which will be linked to the post, or if you’ve using the above function with ACF, it will be linked to an external link. All we have to do to make it open in a new window is check if the post has it’s custom field for the external URL filled. Then we just tell it that if that custom field is filled, we’ll output target=”_blank” within the anchor tag, like this:

<a href=”<?php the_permalink(); ?>” <?php if(get_field(‘CUSTOM FIELD NAME’)) { echo ‘target=”_blank”‘; } ?>><?php the_title(); ?></a>

 

Now if your custom field is filled with external link, the link within the loop will open in a new tab!

Share this article: Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUponPin on PinterestEmail this to someone

6 comments

  • This is great, thank you! How can I also have the external link open in a new tab?

    Reply
    • Hello Nadine! Glad you found this useful. You can have the link open in a new tab by using some conditional logic within the loop. I’ve posted an update to the post.

      Reply
  • thanks so much for the fast update Tyler!

    Reply
  • Thank you, your article really helps! Could you please tell me how to add external links only on a specific page?

    Reply
    • Hey Marcin,

      You can surround the whole output statement for the external field with an if(get_field(‘FIELDNAME’)) {, which will check if that field is filled. If the field isn’t filled, then nothing will be output. Then surround the normal output of the default permalinks to if(!get_field(‘FIELDNAME’)) {. So, for instance, if you have a post that has an external link field in it, but you don’t fill it, then by default the permalink output will be the original WordPress permalink and not the external permalink. However, if you fill the field, then the external permalink will be output. Hope that helps!

      Reply
  • Thank you so much. I am trying to make the external link open up to a new page.

    <a href="” title=”” rel=”nofollow” id=”featured-thumbnail”>

    <a href="” title=”” rel=”nofollow” id=”checkout” class=”button”>

    ANy idea where I add the blank command?

    Thank you

    Reply