
Add an Options Page with ACF (Advanced Custom Fields)
Over the course of the last eight years, I’ve built over 150 sites, most using WordPress.
This code snippet is something I’ve used with ACF PRO aka Advanced Custom Fields PRO in order to set up an Options Page.
The data from this newly created Options Page would be saved in the wp_options
database table. It is not saved in the wp_posts
table.
This allows you to use the custom fields data in the header of your site, the footer, or on any page from a single input source. In the past, I’ve used this to set up universal options, like Social Media URLs, or information for a Prefooter.
Drop this code in your functions.php
file, and you can add Custom Field Groups for the Options Page.
/**
* Add ACF Options page.
*/
if( function_exists('acf_add_options_page') ) {
acf_add_options_page();
}
You’ll need to create an Options Field Group, and set up custom fields.
To display values from a custom field from the Options Page, use this format:
/**
* Display custom field from the Options Page.
*/
<?php the_field('your_custom_field', 'options'); ?>
/* Or use this */
<?php get_field('your_custom_field', 'options'); ?>
/* Or you can also use this */
<?php
//variables
$yourfield = get_field('your_custom_field', 'options');
if($yourfield) :
echo $yourfield;
endif;
?>
The first parameter is the name of the custom field, the second parameter is the post_id
, which for the Options Page, needs to be the string 'option'
or 'options'
.
More information on how to use the Options Page in ACF is located on the ACF site.