Support

Account

Home Forums General Issues Redirect to File URL

Solving

Redirect to File URL

  • Hi, I have a ACF file field on a custom Post Type Resources, let’s call one of the posts, “PDF Post”. Whenever a user goes to the URL for “PDF Post”, I want them to be redirected straight to the File URL.

    This will be applied throughout the Resources Post Type so that my users can use the Post URLs to go straight to the attached file.

    I’m using this for the Redirect PHP.

    <?php  
    header("Location: ".$newURL, true, 301);  
    exit();  
    ?> 

    What’s the best way to return the insert the URL returned by the ACF File Field into the Redirect PHP?

    Any help is appreciated. Thanks!

  • I am a bit confused by the question, is this sort of what your going for?

    function redirect_to_acf_file_url() {
        // Check if it is a singular "Resources" post
        if (is_singular('resources')) {
            // Get the current post ID
            $post_id = get_the_ID();
    
            // Get the URL from the ACF file field, assuming the field name is 'file_url'
            $file_url = get_field('file_url', $post_id);
    
            // Check if the URL is not empty
            if (!empty($file_url)) {
                // Perform the redirect
                wp_redirect($file_url, 301);
                exit;
            }
        }
    }
    
    add_action('template_redirect', 'redirect_to_acf_file_url');
    
  • Hi Andrew,

    Thanks for your help regardless of the confusion.
    We’re close! I’m using a Snippets plug-in to insert the PHP to the header of these specific Custom Post Types we don’t have to solve that.
    Therefore, I was really looking for that second part which Checks if the URL is not empty and Performs the redirect. The

    <?php>  function redirect_to_acf_file_url() {
            // Get the URL from the ACF file field, assuming the field name is 'file_url'
            $file_url = get_field('file_url', $post_id);
    
            // Check if the URL is not empty
            if (!empty($file_url)) {
                // Perform the redirect
                wp_redirect($file_url, 301);
                exit;
            }
        }
    
    add_action('template_redirect', 'redirect_to_acf_file_url');
    ?>

    I changed my own Field Name to file_url to match the code. Just to clarify, the Field on ACF is in fact a File field, these files are usually PDF.

    When the Resource post is accessed, we want them to bypass the post, straight into the URL of PDF file within the post, seclected through ACF.

    Let me know if there’s any more clarification needed.
    Thanks!

  • I was able to get this to work:

    <?php
    function redirect_to_acf_file_url() {
      $file_url = get_field('file');
      if (!empty($file_url) && is_string( $file_url ) ) {
        wp_redirect( esc_url( $file_url ), 301);
        exit;
      }
    }
    redirect_to_acf_file_url();
    ?>

    But curiously, now when I add a New Post and attach a file, the same way I’ve done in the past. The Redirect doesn’t work on the new post. When I change an select a new File to attach in an Existing Post, it similarly breaks also.

    Please help!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.