Search code examples
phpwordpresscontact-form

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?


Solution

  • 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; 
     ?>