• 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

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