Last active
August 28, 2019 09:14
-
-
Save graham73may/8c480bda978a5475f9018c6bce2c9f12 to your computer and use it in GitHub Desktop.
Rewrite a single's permalink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Linking for "Ghost" post types | |
* | |
* @param $url | |
* @param $post | |
* @param bool $leavename | |
* | |
* @return mixed | |
*/ | |
if (!function_exists('edit_news_permalink')) { | |
add_filter('post_link', 'edit_news_permalink', 10, 3); | |
add_filter('post_type_link', 'edit_news_permalink', 10, 3); | |
function edit_news_permalink($url, $post, $leavename = false) | |
{ | |
if ($post->post_type == 'news' && get_field('is_external', $post)) { | |
$external_url = get_field('external_url', $post); | |
if (!empty($external_url)) { | |
$url = $external_url; | |
} | |
} | |
return $url; | |
} | |
} | |
/** | |
* Redirecting for the News single template | |
*/ | |
if (!function_exists('redirect_external_content')) { | |
add_action('template_redirect', 'redirect_external_content'); | |
function redirect_external_content() | |
{ | |
if (is_singular('news')) { | |
global $post; | |
if ($post->post_type == 'news' && get_field('is_external', $post)) { | |
$external_url = get_field('external_url', $post); | |
if (!empty($external_url)) { | |
wp_redirect(esc_url($external_url), 301); | |
exit; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment