Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Advanced Custom Fields and Contact Form 7 to display a form

I want my users to be able to put a Contact Form 7 shortcode into a custom field in the Wordpress editor. I've created the custom field using ACF and I can pull the value onto the page, but when I try to include it in the shortcode, it comes back with a 404.

This code:

<?php echo do_shortcode(get_field('contact_form_shortcode')); ?>

Returns:

[contact-form-7 404 "Not Found"]

If I create a variable out of the value like this:

<?php
 $formCode = get_field('contact_form_shortcode');
 echo $formCode;
?> 

The echo returns:

[contact-form-7 id="473" title="Learn More Form"]

But I get the same 404 after putting that value into the echo do_shortcode function list this:

<?php echo do_shortcode($formCode); ?>

What am I missing?

like image 757
ReLeaf Avatar asked Dec 20 '22 11:12

ReLeaf


2 Answers

To do it With ACF pro plugin and without other extra plugins.

  1. Create a relation field ( example: contact_form )
  2. add the below code into your page loop:

    <?php $posts = get_field('contact_form');
       if( $posts ): 
         foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) 
           $cf7_id= $p->ID;
           echo do_shortcode( '[contact-form-7 id="'.$cf7_id.'" ]' ); 
         endforeach;
       endif; ?>
    

Create ACF relation field

like image 183
Abouasy Avatar answered Feb 13 '23 03:02

Abouasy


I was able to resolve this issue by using the technique I discussed in my comment above. By using the WYSWIG field set to 'Run filter "the_content"' I'm able to pull the field value in the way I want it. The only drawback is that users could type something else in there besides a form shortcode.

Here's my final code:

<?php
    if (get_field('contact_form_shortcode')):
        echo get_field('contact_form_shortcode');
    else:
        echo do_shortcode('[contact-form-7 id="473" title="Learn More Form"]');
    endif; 
 ?>
like image 27
ReLeaf Avatar answered Feb 13 '23 04:02

ReLeaf