Search code examples
phpwordpressadvanced-custom-fieldscontact-form-7

Dynamically download correct pdf from file custom field after contact form 7 submission based on page you're in


Hi, i have a site that displays various projects as posts, each project has its own pdf file, uploaded via a file custom field for that post. For the moment there's just a dynamic button as you can see in this example: https://www.stefanomengoli.it/sm21/progetti/vivere-nelle-nuvole-progetto-di-bioarchitettura-per-un-loft-casa-sullalbero/

What i need is the user to be able to download or be redirected to the correct file after CF7 submission based on the project he/she is on.

I tried this code and it works with a specific url, but what i need is to put an acf url that dynamically shows the correct file as I said based on the project the visitor is on.

add_action( 'wp_footer', 'example_download' );

function example_download() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
if ( '946' == event.detail.contactFormId ) {
    window.open('https://www.example.com/wp-content/uploads/2021/05/my-document.php', 
'_self');    
}
}, false );
</script>
<?php
}

Solution

  • You just need to echo the custom field in the window.open

    This should work. Just replace your_field with your ACF field name

    add_action( 'wp_footer', 'example_download' );
    function example_download() {
        global $post;
        ?>
        <script type="text/javascript">
            document.addEventListener('wpcf7mailsent', function (event) {
                if ('946' == event.detail.contactFormId) {
                    window.open('<?php the_field('your_field', $post->ID);?>',
                        '_self');
                }
            }, false);
        </script>
        <?php
    }