• Shopping Cart Shopping Cart
    0Shopping Cart
Storyteller
  • SEO
    • SEO Cape Town
    • Free SEO Audit
    • Free SEO Price Calculator
    • SEO
  • PPC
    • Google Ads Agency
    • Ecommerce Google Ads
    • Cost Calculator
  • Websites
    • Identity Websites
    • WordPress Speed Optimisation
    • Doctors Websites
    • Doctors Logos
    • Portfolio
  • Blog
  • Contact
  • Menu Menu
Blog, Development, Featured, News, PHP, WordPress

How to create a shortcode using Advanced Custom Fields ( ACF ) data

ACF Fields With Shortcodes

I have noticed that when Google populates the search results for this question there are actually two different questions being asked by a similar search.

The first asks:

How do I create a shortcode that outputs the value of an Advanced Custom Fields (ACF) plugin field.

The second asks:

How can I dynamically populate a short code using the Advanced Custom Fields plugin.

I was trying to find the answer to the first question, but I will try to show both answers here, so that you do not have to search again.

How do I create a shortcode that outputs the value of an Advanced Custom Fields plugin field.

 

  1. To do this you must first install and activate the ACF plugin on your WordPress website. You can download the free version here.
  2. Create a field group with the information you want to use. You can learn more about how to do this on the ACF website.
  3. Once you have your field set up you can start creating your shortcode. You are going to do this in the functions.php file of your child theme.
    • First create a function. Here is an example<
      function my_shortcode() { 
      
      	$myfield = get_field('my_field',false,false);
      	
      	return $myfield ;
      	
      }
      

      The trick here is to first create a variable using the get_field ACF function and then return that value as the output for the function./li>

    • Once that is done you need to register your function as a shortcode.
      add_shortcode( 'myshortcode', 'my_shortcode' );
      
    • Putting it all together you get
      function my_shortcode() { 
      
      	$myfield = get_field('my_field',false,false);
      	
      	return $myfield ;
      	
      }
      add_shortcode( 'myshortcode', 'my_shortcode' );
      

It is important to note that you only need to worry about this if you are using more advanced field than just a text field. If you are only using a text field you can use the inbuilt ACF shortcodes.

e.g.

[acf field="field_name" post_id="123"]

You can learn more about that here.

How can I dynamically populate a short code using the Advanced Custom Fields plugin.

 

Since I didn’t have to actually code this, I am taking it from an example I found on StackOverflow. You can read it here.

Say you have an ACF field ‘contact_form’ which is the ID of a contact form 7 form. You want to populate this ID dynamically using ACF.

Your normal short code that you would use in a template is

echo do_shortcode( '[contactform id="1457"]' );

To get this to work is as simple as

echo do_shortcode( '[contactform id="'.get_field('contact_form').'"]' );

The trick here is to use the ACF get_field() function instead of the ACF the_field() function.

Please note that the contact form 7 short code has been changed so that it is not called here, and you can see how we use it. Otherwise we get a 404 contact form shortcake showing.

I hope this helps.

If you have any questions please leave a comment below.

May 16, 2018/1 Comment/by clydet
https://i0.wp.com/storyteller.co.za/wp-content/uploads/2018/05/ACF-Fields-With-Shortcodes.jpg?fit=600%2C600&ssl=1 600 600 clydet https://storyteller.co.za/wp-content/uploads/2019/12/Storyteller-Logo-New.png clydet2018-05-16 22:09:592020-03-20 21:46:32How to create a shortcode using Advanced Custom Fields ( ACF ) data
AdWords, Blog, Contact Form 7, Conversions, Development, Featured, Javascript, PHP, SEO, WordPress

How to redirect Contact Form 7 to a thank you page.

How To Redirect Contact Form 7 To Thank You Page

UPDATED 14-02-2019

We love using the WordPress plugin Contact Form 7 for the websites we build for our clients. It is extremely easy to use but very powerful and has reasonable documentation. A big plus for us is that it is a much loved plugin in the WordPress community and so answers and solutions can be found for almost all issues on sites like Stack Overflow and WordPress support.

At Storyteller Design we help out clients drive traffic to their sites through SEO and Google AdWords as well as the other search engine marketing like Bing Ads. One of the most important metrics for us is the conversion of these visitors to our client’s sites. A conversion is whenever someone completes a contact form, downloads a pdf or information sheet or calls the client.

Contact Form 7 How To Redirect To Thank You Page

We use Contact Form 7 for most of our contact forms and download forms. Once the form has been completed and the submit/download button clicked we want to redirect the visitor to a page where we can log that conversion in Google Analytics and pass that information back to our Google AdWords campaign.

Contact Form 7 used to suggest using an easy copy and paste technique to redirect to your thank you page. However this method has been depreciated by Contact Form 7 and will no longer work after the end of 2017. This leaves two options.

See Old Depreciated Method

Go to the “Contact” menu item in your WordPress back end side bar, click on the form you want to redirect and then click on the “Additional Settings” tab.

Add this code into the field provided:

[javascript]
on_sent_ok: "location = ‘http://www.example.com/thank-you/’;"
[/javascript]

You need to replace the http://www.example.com/thank-you/ with the url of your thank you page.

 

  1. If you are not familiar with coding you can use a plugin called Contact Form 7 Redirection. They have both a free option available through WordPress but their Pro version adds some fantastic functionality that we often use for our clients.

Contact FOrm 7 Redirection

This plugin uses the “wpcf7_mail_sent” action hook to redirect to the thank you page, and this hook shouldn’t be depreciated soon. This plugin has been tested to WordPress version 5.0.3 so it will continue to work well for you. It requires Contact Form 7 version 4.8 or later.

We suggest you try this plugin first and if it doesn’t work then move onto the second option.

  1. Use some php in your child-themes functions.php file.

The method now recommended in the Contact Form 7 documentation is to use javascript to redirect when the wpcf7mailsent event occurs. The code to use is:

<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'http://yourdomain.com/thank-you';
}, false );
</script>

If you only use one contact page, or only use on thank you page then all you need to do copy and paste the code below into your child theme’s function.php file.

//This function prints the JavaScript to the footer
function cf7_footer_script(){ ?>
 
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'http://yourdomain.com/thank-you';
}, false );
</script>
 
<?php } 
 
add_action('wp_footer', 'cf7_footer_script'); 

Again remember to replace the http://example.com/thank-you with your thank you page.

However we often use multiple contact form 7 forms on a website with multiple thank you pages. This is because we want to know exactly which pages, forms and calls to actions are converting our website visitors into leads so we send more traffic there, and optimise our conversion funnels.

In order to redirect different contact form 7 forms to different thank you pages you need to use the code below:

<?php
//This function prints the JavaScript to the footer
function cf7_footer_script(){ 

//if page name is contact.
if ( is_page('contact')) {?>
 
	<script>
	document.addEventListener( 'wpcf7mailsent', function( event ) {
			location = 'http://example.com/thank-you';
		}, false );
	</script>
 
<?php } 
	
}
add_action('wp_footer', 'cf7_footer_script');

Here is an example of the code for two different pages.

<?php

//This function prints the JavaScript to the footer
function cf7_footer_script(){ 

//if page name is contact.
if ( is_page('contact')) {?>
 
	<script>
	document.addEventListener( 'wpcf7mailsent', function( event ) {
			location = 'http://example.com/thank-you';
		}, false );
	</script>

<?php } else if ( is_page('download')) /* if page name is download */ {?>
 
	<script>
	document.addEventListener( 'wpcf7mailsent', function( event ) {
			location = 'http://example.com/thank-you';
		}, false );
	</script>
 
<?php } 
	
}
add_action('wp_footer', 'cf7_footer_script');

We hope this helps. It is important to note that this code will redirect all contact forms one a page to the same thank you page.

Please let us know if you have any questions in the comments below.

How to redirect contact form 7 to thank you page

October 16, 2017/1 Comment/by clydet
https://i0.wp.com/storyteller.co.za/wp-content/uploads/2017/10/Blog-Featured-Image-CF7.jpg?fit=900%2C600&ssl=1 600 900 clydet https://storyteller.co.za/wp-content/uploads/2019/12/Storyteller-Logo-New.png clydet2017-10-16 10:57:222019-06-24 17:35:08How to redirect Contact Form 7 to a thank you page.

Grow Your Business Online

Google Partner



Need Help With Your Website?

Get a beautiful done for you website without the high cost of custom web design!

Our identity websites include:

  • 1 - 5 Page WordPress Website
  • 1 hour of website updates and maintenance each month
  • On-page SEO
  • Website Security
  • Website Hosting
  • emplate Design
  • Google Analytics Integration
  • Website Backups

Follow Us On Facebook

Identity Websites R499pm

  • southernoncology
    southernoncology.co.zaJune 8, 2022 - 17:12
  • thetravelstation.co.za homepage
    thetravelstation.co.zaFebruary 14, 2022 - 16:07
  • Ortho Cape Town SEO
    orthocapetown.comFebruary 14, 2022 - 15:28
  • DSTV Nation - Identity Website
    dstvsolutions.co.zaFebruary 19, 2021 - 11:46
  • Marriage Officer Lara - Identity
    marriageofficerlara.co.zaFebruary 19, 2021 - 11:28
  • Blazed and infused - Identity
    blazedandinfused.co.zaFebruary 19, 2021 - 11:15
  • Botanicum - Identity
    botanicum.co.zaFebruary 19, 2021 - 11:09
  • Elope Cape Town - Identity
    elopesouthafrica.comFebruary 19, 2021 - 10:38

Featured Posts

  • Rubber Roofs Social Media Case Study
    @rubberroofsza: brand building and sales growthNovember 2, 2021 - 11:05
  • A5 Social Media FI
    @a5_cash_and_carry: exponential growth and online purchasesJanuary 22, 2021 - 16:30
  • Dr Chiba Social Media Case Study
    @drgaveetachiba: steady growth and a new income streamJanuary 15, 2021 - 14:48
  • How To Get More Google Reviews
    How to Get More Google Reviews for Your BusinessJune 25, 2018 - 19:28
  • ACF Fields With Shortcodes
    How to create a shortcode using Advanced Custom Fields ( ACF ) dataMay 16, 2018 - 22:09

Top Pages

  • Free SEO Audit
  • Identity Websites
  • Contact
  • Home

Social

  • View storytellerdesigncapetown’s profile on Facebook
  • View storytellerdgn’s profile on Twitter
  • View storyteller.design’s profile on Instagram
  • View UCzu-Pa_fRtS5P9W_f94Ng_A’s profile on YouTube
  • View StorytellerDesign’s profile on Google+
  • View 147834324@N03’s profile on Flickr
  • View storytellerdesign’s profile on Tumblr

Social

  • View storytellerdesigncapetown’s profile on Facebook
  • View storytellerdgn’s profile on Twitter
  • View storyteller.za’s profile on Instagram
  • View storyteller-design-6a9429143’s profile on LinkedIn
  • View UCzu-Pa_fRtS5P9W_f94Ng_A’s profile on YouTube
  • View StorytellerDesign’s profile on Google+
  • View 147834324@N03’s profile on Flickr
  • View storytellerdesign’s profile on Tumblr

Important Pages

  • Careers
  • Terms & Conditions
  • Privacy Policy

Listed on Top 10 SEO and WebCompare

Find Us

Address: Storyteller, Workshop 17, 17 Dock Road, V&A Waterfront, Cape Town, 8005

Contact Us

Email: [email protected]
WhatsApp Us
Skype: thomas_clyde

Storyteller Logo New
© Copyright - Storyteller | Our Sites: Storyteller AUS | Solarenergyseo.com
Scroll to top Scroll to top Scroll to top

This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies.

Accept settingsLearn More

Cookie and Privacy Settings



How we use cookies

We may request cookies to be set on your device. We use cookies to let us know when you visit our websites, how you interact with us, to enrich your user experience, and to customize your relationship with our website.

Click on the different category headings to find out more. You can also change some of your preferences. Note that blocking some types of cookies may impact your experience on our websites and the services we are able to offer.

Essential Website Cookies

These cookies are strictly necessary to provide you with services available through our website and to use some of its features.

Because these cookies are strictly necessary to deliver the website, you cannot refuse them without impacting how our site functions. You can block or delete them by changing your browser settings and force blocking all cookies on this website.

Google Analytics Cookies

These cookies collect information that is used either in aggregate form to help us understand how our website is being used or how effective our marketing campaigns are, or to help us customize our website and application for you in order to enhance your experience.

If you do not want that we track your visist to our site you can disable tracking in your browser here:

Other external services

We also use different external services like Google Webfonts, Google Maps and external Video providers. Since these providers may collect personal data like your IP address we allow you to block them here. Please be aware that this might heavily reduce the functionality and appearance of our site. Changes will take effect once you reload the page.

Google Webfont Settings:

Google Map Settings:

Vimeo and Youtube video embeds:

Privacy Policy

You can read about our cookies and privacy settings in detail on our Privacy Policy Page.

Privacy Policy
Accept settingsHide notification only