Support

Account

Forum Replies Created

  • 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!

  • 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!

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