Home Forums General Issues Action before get_field function


Action before get_field function

  • Hi,

    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 and acf/format_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.

  • I would like to reduce all calls to the database. That is, all field values stored in files and download them from the files – not from the database.

  • 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 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.

  • Thanks your for answer! Unfortunately It doesn’t work with get_field(‘xxx’, ‘option’).

  • 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.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Action before get_field function’ is closed to new replies.