Support » Plugin: ACF Field For Contact Form 7 » can’t get form id and use in template?

  • Resolved rtpHarry

    (@rtpharry)


    I’m trying to use the cf7 field in a template to show the contact form, but then in some backend code I also need the form ID.

    The docs show how to enable the object mode but not how to remove it again.

    I’ve tried this but it doesn’t work:

    function get_voippdf_form_id() {
    	function get_acf_cf7_object () {
    		return true;
    	}
    
    	add_filter('acf_cf7_object', 'get_acf_cf7_object', 10);
    	$form = get_field('voippdf_contact_form', 'option');
    	remove_filter('acf_cf7_object', 'get_acf_cf7_object', 10);
    	return $form->id;
    }

    Once the add_filter has been called in the function above, then my template in footer.php which also calls get_field, returns the object instead of the form and breaks the page.

    Is there some way to use both the form and the object in a single page load?

    • This topic was modified 10 months, 1 week ago by rtpHarry.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter rtpHarry

    (@rtpharry)

    I’ve tried using https://www.advancedcustomfields.com/resources/get_field_object/ but that doesn’t contain the form ID inside of it.

    Plugin Contributor Dilip Bheda

    (@dilipbheda)

    Hello, @rtpharry
    Add the below code in your current theme functions.php file.

    function get_acf_cf7_object() {
        return true;
    }
    add_filter( 'acf_cf7_object', 'get_acf_cf7_object' );

    Now you can access contact form 7 object like this

    $cf7_form = get_field( 'field_name' );
    
    if ( $cf7_form ) {
      echo $cf7_form->id;
      echo [contact-form-7 id="$cf7_form->ID" title="Contact form 1"]
    }

    Thanks

    Thread Starter rtpHarry

    (@rtpharry)

    Ohhh! Good idea haha, I forgot about the shortcode.

    In my head I was thinking you must be doing some script registration and stuff in the background to inject the form in using your plugin, and it would be complicated to manually inject the form, but it’s all handled by the shortcode.

    Plugin Contributor Dilip Bheda

    (@dilipbheda)

    @rtpharry Please provide the rating to the plugin.
    Have a nice day!

    Thanks

    Thread Starter rtpHarry

    (@rtpharry)

    FYI if anyone else is looking for this, I just tidied up the snippet a bit:

    $cf7_form = get_field( 'field_name' );
    
    if ( $cf7_form ) {
      echo $cf7_form->id;
      echo '[contact-form-7 id="'.$cf7_form->id'." title="Contact form 1"]';
    }

    hello sir,
    In my project “https://snehaclasses.com/download/” we want to download pdf after submit cf7.
    we use ACF file field.
    on click download button pop bootstrap modal is opne with cf7 after fill cf7 I want to auto download correct file according to subject.
    what can I do For this plz help me.

    <div class="row">
                         <?php
                         wp_reset_postdata();
                          $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                          $args =array(
                          'post_type' =>array( 'sample_papers'),
                          'category_name' => 'jee', 
                          'posts_per_page' => -1 
                           );
                          $postslist = get_posts( $args );
                          $i = 0;
                          foreach ($postslist as $post) :  setup_postdata($post);
                          $featuretitle = get_the_title($post->ID);
                          $post_id    = get_the_id();
                          $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );   
                          $i++;
                          ?>
    
                         <tr>
                            <td> <?php the_field('sample_paper')?></td>
                            <td>
                            <div class="col-md-12 d-flex align-items-stretch" >
                                <div class="modal-toggle" data-toggle="modal" data-target=".modal1<?php echo $i;?>">
                                    <div class="card-body">
                                        <?php
                                        $file = get_field('upload');
                                        if( $file ): ?>
                                        <span data-id="<?php the_ID(); ?>" class="card-title view-post" style="cursor: pointer;  position: absulate;right:0 !important;">Download</span> </p>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </td>
                        </tr> 
                            </div>
                        <div class="modal modal1<?php echo $i;?>" id="post<?php echo $post_id; ?>">
                            <div class="modal-dialog modal-lg modal-dialog-centered">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="postModalLabel"><?php the_field('sample_paper') ?></h5>
                                         <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                      </button>
                                    </div>
                                    <div class="modal-body">
                                        <form action="" method="get" class="php-email-form">
                                            <?php echo do_shortcode('[contact-form-7 id="523" title="Contact form 1"]');?>
                                        </form>
                                    </div>
                                 </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                  
                    </div>

    in function.php I also use

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

    Can we do it with ypur plugin plz help me out.

    Plugin Contributor Dilip Bheda

    (@dilipbheda)

    @bharti9107 The plugin is simply providing a field for the admin to select the contact form. Your query is related to a feature you wish to build not a support request/issue with the plugin.

    Thank you

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘can’t get form id and use in template?’ is closed to new replies.