Aleš Sýkora / November 28, 2023 / 0 comments

How to Retrieve ACF Field from a Group field within an Option Page

Post summary: If you want to use ACF fields data from option page – it’s easy. You can just use get_field(‘field_slug’,’option’) unless you wrap the field in the group field. Let’s take a look how to get the field data. Understanding the Structure of Advanced Custom Fields Before we dive into the specifics, let’s take a moment…

If you want to use ACF fields data from option page – it’s easy. You can just use get_field(‘field_slug’,’option’) unless you wrap the field in the group field. Let’s take a look how to get the field data.

Understanding the Structure of Advanced Custom Fields

Before we dive into the specifics, let’s take a moment to understand the structure of Advanced Custom Fields. At its core, ACF revolves around two primary elements: Field groups, fields and groups. Fields represent individual pieces of data, such as text inputs, checkboxes, or image uploads. Field Groups, on the other hand, act as containers for these fields, allowing you to organize and manage them efficiently and ACF Group field can organize fields inside the Field Groups.

Creating an Option Page

To retrieve an ACF field from a group field in an option page, the first step is to create the option page itself. Option pages serve as a centralized location for storing and accessing various settings or configurations for your WordPress website. By following these simple steps, you can create an option page using ACF:

  1. Install and activate the Advanced Custom Fields PRO plugin
  2. In your theme’s functions.php file, add the following code to create a new option page:
if (function_exists('acf_add_options_page')) {
    acf_add_options_page(array(
        'page_title' => 'Theme fields',
        'menu_title' => 'Theme fields',
        'menu_slug' => 'theme-fields',
        'capability' => 'edit_posts',
        'redirect' => false
    ));
}

You can create subpages of your Option page as you need. Check the Acf Option page official documentation for more information.

Creating a Group field for ACF Option page

After you create the Option page, it should be visible in your WordPress admin center sidebar.

That’s great! Let’s add your first field Group with Group field and text field inside the group field. Don’t forget to set location rule to display on your Option page.

  1. Navigate to ACF > Field Groups > + Add new
  2. Name your field group
  3. + Add field -> Group
  4. + Add field inside Group
  5. Set the location conditional to Option page

Retrieving a Field from Option page Group field

Once you have set up the option page, you can proceed with retrieving an ACF field value from a group field. To accomplish this, follow these steps:

  1. Identify the Group field slug: Each group created in ACF has a unique identifier known as the Field Name (slug). This key is essential for referencing the group field when retrieving its fields.
  1. Access the Group Field content: To retrieve the values of a specific group field, you need to use the get_field() function provided by ACF. The get_field() function requires two arguments: the field name ‘option’ parameter.
  2. Access value of field inside the Group Field: You will need the slug of field wrapped in the group

Here’s an example of how you can retrieve the value of an ACF field from an option page group field:

<?php
$group_field_value = get_field('your-group-field', 'option');
$field_value = $group_field_value['inner_field'];
// Output the value of the field
echo $field_value;
// echo do_shortcode( $field_value ); --if you store a shortcode in ACF text field, you can run it with this
?>

Remember to replace 'your-group-field' and 'inner_field' with the actual group field name (slug) and the name of field inside the group field.

The key to unlocking ACF’s true potential lies in understanding its structure and utilizing its features effectively. By implementing the steps outlined in this guide, you’ll be well on your way to harnessing the power of ACF.

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

0 comments

Share Your Thoughts