Advanced Custom Fields – External Permalink Field

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', 'tjdesign_permalink_links', 10, 2 );
function tjdesign_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:

tjdesign_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 me 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!

Leave a Reply

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