0

I want to achieve the following:

  • On a post or a page, I can define an optional custom redirect URL (possibly as an Advanced Custom Field)
  • If I view this post/page with a specific added URL parameter (like "?redirect=true", send a redirect to the defined custom URL, otherwise do not redirect.

My naive approach would be to add this as a javascript to the post/page template.

Is there a better option? Or a plugin I could (ab)use for that purpose?

1

It should not be tough. As you suggested you can use ACF to add a custom field in post/page and then use the template_redirect action hook to check if the current page's custom field is a valid URL as well as $_GET['redirect'] is true, wp_redirect to that URL.

function my_page_template_redirect()
{
    if( is_url(GET_ACF_FIELD_VALUE( 'CUSTOM_FIELD' )) && !empty($_GET['redirect']) && $_GET['redirect'] == 'true)
    {
        wp_redirect( GET_ACF_FIELD_VALUE( 'CUSTOM_FIELD' ) );
        exit();
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );
2
  • Thanks! As I'm a WP noob (from a customization point of view) - where do I need to put this?
    – thomers
    Mar 18 '15 at 16:01
  • 1
    You need to have little bit knowledge for such customizations. Above code has a template function "GET_ACF_FIELD_VALUE( 'CUSTOM_FIELD' )" which is supposed to be replaced with respect to your implementation of custom field. Above code should be pasted in your theme's functions.php
    – M-R
    Mar 18 '15 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.