Support

Account

Home Forums Feature Requests Hook – Save Post by Group

Helping

Hook – Save Post by Group

  • Hello,

    I’ve found myself using ACF a lot for frontend forms or admin option pages both of which usually connect to custom database tables. One issue I consistently run into is making sure that my pre_save_post or save_post hooks don’t run into any other forms specific hooks. For example, if I have 2 separate forms on 2 separate pages both of which submit data to a custom table, I need to jump through some conditional hooks in each callback to ensure I’m submitting data for the correct form.

    Sometimes I check against the global post type or the given $post_id type. Sometimes I utilize a custom function to ensure that the posted field key is of the expected group. Both of which are fine I guess.

    Since WordPress has hooks for save_post_{$post_type} I thought it could be a good idea to add in additional hooks based on the group key. I say group key specifically because in the case of localized fields (PHP exported fields or JSON), group key is the main “entry point”.

    acf/pre_save_post/?group_key=123 or something similar.

    Maybe I’m missing a function that does this automagially. Open to suggestions!

  • As far as I know ACF never looks at the group a field is in when saving. ACF does not save field in group order. It looks at $_POST['acf'][$field_key] at the top level and works through the array, recursing into sub arrays.

    The field group that each field is in is not part of the submitted data.

    Finding the real group key could be potentially problematic. Take for example a field that is cloned. If you look at the parent and recurse up you will get to the group that the field lives in before being cloned. The only way to get to the field from the group that it is cloned into is to recurse over fields and sub fields starting at the top. This is provided by the recursive nature of the input

    
    $_POST['acf']['field_XXXXX'][0]['field_YYYYY'][0]['field_ZZZZZ'][etc]
    
    

    in order to provide this hook ACF would need to, at the top level, record the group key of that field and then refer to that when saving each field. There could be multiple groups being saved so ACF would need to have a hook for every field. Also do to the recursive nature there isn’t any way to know at what level the loop over fields is being done. Instead of reading the inputs sequentially ACF would need to first read through them, sort them by group and then loop over the fields in each group. Or the hierarchy of the submitted data would need to be altered to something like
    $_POS['acf'][$group_key][0][$field_key] which would result in completely breaking backwards compatibility. And even here there would be an issue with cloned fields I think.

    I do know what you are talking about, every acf/save_post action I create has something at the top that checks the passed $post_id value against something before it does anything and returns if it’s not what it needs to be. But knowing how ACF looks at the submitted fields I don’t see how anything else could be possible.

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

You must be logged in to reply to this topic.