acf_add_options_page()
- Description
- Parameters
- $settings
- Return
- Examples
- Default options page
- functions.php
- Customized options page
- functions.php
- Creating sub pages
- functions.php
- Notes
- Priority
- WordPress 6.8
Description
Adds an options page to the admin menu.
Options pages are used to store global settings. These settings are not tied to a specific post, but are instead stored in the wp_options table.
Once registered, your page will appear in the admin menu. You can then assign fields to your page via the “Options Page” location rule when editing a field group.
Parameters
acf_add_options_page( [$settings] );
$settings
(array) (Optional) Array of arguments for registering an options page.
– page_title
(string) The title displayed on the options page. Defaults to ‘Options’.
‘page\_title’ => \_\_(‘My Options’),
Return
(array) The validated and final page settings. This is useful to find the page’s menu_slug and use it later when adding child options pages.
Examples
Default options page
This example shows how to create the default options page.
functions.php
add_action('acf/init', function() {
if( function_exists('acf_add_options_page') ) {
acf_add_options_page();
}
} );
Customized options page
This example shows how to create a customized options page and store the data in a variable for later use.
functions.php
add_action('acf/init', 'my_acf_op_init');
function my_acf_op_init() {
// Check function exists.
if( function_exists('acf_add_options_page') ) {
// Register options page.
$option_page = acf_add_options_page(array(
'page_title' => __('Theme General Settings'),
'menu_title' => __('Theme Settings'),
'menu_slug' => 'theme-general-settings',
'capability' => 'edit_posts',
'redirect' => false
));
}
}
Creating sub pages
This example shows how to create an options sub page using the parent_slug attribute.
functions.php
add_action('acf/init', 'my_acf_op_init');
function my_acf_op_init() {
// Check function exists.
if( function_exists('acf_add_options_page') ) {
// Add parent.
$parent = acf_add_options_page(array(
'page_title' => __('Theme General Settings'),
'menu_title' => __('Theme Settings'),
'redirect' => false,
));
// Add sub page.
$child = acf_add_options_page(array(
'page_title' => __('Social Settings'),
'menu_title' => __('Social'),
'parent_slug' => $parent['menu_slug'],
));
}
}
Notes
Priority
This function must be used before the action admin_menu (priority 99) as this is when admin pages are registered in WordPress core. We advise using the “acf/init” action.
WordPress 6.8
Since WordPress 6.8, any function that causes ACF to initialise early such as this one will trigger an PHP notice, if you call it before the acf/init action:
Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the acf domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later.
To avoid this, you must make sure you don’t call any ACF functions before the acf/init action. Our code samples above show how to wrap these calls appropriately.