How do I perform action before starting function get_field or replace it with own function?
I would like to keep name of the function get_field. Edit file api-template.php is not an option.
I need this to extend to the ACF, which I create. I would like to before the call get_field function perform my custom function. Then either the function completes and returns a value or I will return my custom value and function fails to execute.
Thank you in advance for your help!
This is not possible. You’ll either need to choose a different function or use the available hooks. What you want to do is why there are hooks in ACF that let you filter values, for example acf/load_value https://www.advancedcustomfields.com/resources/acfload_value/ and acf/format_value https://www.advancedcustomfields.com/resources/acfformat_value/.
Instead of calling your field and deciding whether to call the acf function or not you call the acf function and then you can filter the value returned after the fact.
You can’t create a function named get_field() that overrides the ACF function. That’s a PHP thing and not really related to ACF. There aren’t any filters in ACF before the value is called from the database.
However, there is a hook in WP that is run before the data is retrieved from the database https://developer.wordpress.org/reference/hooks/get_meta_type_metadata/ There isn’t much in the way of documentation. You should look at the WP functions that are referenced under “Used by” on in the documentation. You should be aware that this will bypass the WP cache, which means that every time you get a value from a field all the work will need to be done every time unless you do your own caching of values.
There is a hook in WP that runs before the db call, but it’s specific to each option name so you’d need to do add_filter() for every option name. However, there is an setting in option pages that let’s you store option values to a specific post ID. If you save option values to a post then you can use the metadata hook in WP.
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!
📣 “ACF Chat Fridays”— Advanced Custom Fields (@wp_acf) January 31, 2023
The ACF team holds their first open office hours this Friday! Come and talk ACF, and ask questions about building sites with the plugin.
We’d love to see you there!
📆 Friday 3rd Feb - 3pm UTC
👉 Register here - https://t.co/3UtvQbE4CU pic.twitter.com/oTwW9K1XQ0
© 2023 Advanced Custom Fields.