I’ve seen different information about how to add options pages, which I find a bit confusing.
On https://www.advancedcustomfields.com/resources/acf_add_options_page/#examples in the first example, the default options page is created directly in functions.php. In the second example, it’s created using an “acf/init” action hook. Then again, at https://www.billerickson.net/code/acf-options-page/ it is shown to use the standard WordPress “init” hook to add the options page.
Which way is the “correct” one or does it matter, and if so, why?
And on a related note: In the third example at https://www.advancedcustomfields.com/resources/acf_add_options_page/#examples (below the title “Creating sub pages”) it says to use “acf_add_options_page” to add a sub page; isn’t this the job of “acf_add_options_sub_page”? Is the documentation wrong or is this just another way to do it?
What is correct depends on several things.
You can simply call an ACF function in functions.php if ACF is installed as a plugin the standard way that a plugin is installed.
If ACF is installed inside of the theme, which is possible, the only way that the above will work is if ACF is loaded before you call the function.
It is safer to use either the init or acf/init hook. these are close to being the same, but not quite. ACF initializes at priority 10 on the init hook. In most cases using the init or acf/init are the same, again, this depends on how ACF is loaded.
It is possible to cause a premature initialization of ACF by calling some function before ACF has been initialized on the init hook. It is almost always safer to use the acf/init hook to ensure that ACF is initialize normally.
But, for 95% of users none of this matters because ACF is installed normally like other plugins and most calls to ACF will happen in the theme and ACF will already be initialized. All of the methods you’ve seen are interchangeable. The only time this is generally be an issue is if you’re trying to create a plugin that uses ACF. In this case you need to use acf/init to ensure that ACF has been loaded and initialized because in this case you can’t know if ACF will be loaded first or not. This depends on the order that plugins are activated and can change when plugins are updated.
As far as using acf_add_options_page() instead of acf_add_options_sub_page(). Calling the first function with the “parent_slug” set is the same as calling the second function. However, calling the second function without setting “parent_slug” will cause ACF to use the default options page as the parent of the page you are creating.
Hoping this makes sense.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2023 Advanced Custom Fields.