ACF Options Page: Organizing Theme Settings
The acf_add_options_page() and acf_add_options_sub_page() functions from the Advanced Custom Fields (ACF) plugin allow you to create custom settings pages in the WordPress admin panel. As a result, you can manage global theme settings in one place.
Instead of placing configuration fields across different admin screens, you can centralize them on a single options page. This approach makes site management easier. In addition, it helps clients find important settings faster.
For example, you can store contact information, header configuration, social media links, or tracking codes there. Therefore, these values can be reused across multiple templates.
Creating an ACF Options Page in WordPress
First, you need to register the options page in your theme. The example below shows how to do this inside the functions.php file.
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'Theme Settings',
'menu_title' => 'Theme Settings',
'menu_slug' => 'theme-settings',
'capability' => 'edit_posts',
'redirect' => false
));
acf_add_options_sub_page(array(
'page_title' => 'Header Settings',
'menu_title' => 'Header',
'parent_slug' => 'theme-settings',
));
}
Here, the acf_add_options_page() function creates a main page called Theme Settings. Then, the acf_add_options_sub_page() function adds a subpage called Header Settings.
As a result, your theme configuration becomes easier to organize. Administrators can quickly navigate between different groups of settings.
Adding Fields to the Options Page
- First, open Advanced Custom Fields in the WordPress admin panel.
- Next, go to Field Groups and click Add New.
- Create a new group. For example, you can name it Contact Details.
- Then go to the Location settings. Select Options Page and choose the page you created.
- Finally, add a new field. For instance, create a Text field called Phone and save the group.
Displaying the Field in a Theme Template
Once the field is created, you can display it in your theme template. Use the following code:
<?php
$phone = get_field('phone', 'option');
if ($phone) {
echo 'Phone: ' . $phone;
}
?>
This code retrieves the value from the ACF options page. If the field contains a value, it will be displayed in the template.
In practice, this method is very useful. You can store global information once and reuse it across the entire website.
In short, ACF Options Pages help you build a centralized theme settings panel. Moreover, they make WordPress projects easier to maintain and manage.