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

ReLeaf picture ReLeaf · Jan 17, 2013 · Viewed 21.6k times · Source

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?

Answer

Abouasy picture Abouasy · Apr 15, 2018

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