Support

Account

Home Forums Front-end Issues Prevent acf_form_head Being Added Twice

Solved

Prevent acf_form_head Being Added Twice

  • So I’ve just been debugging an issue in my plugin where I’m using acf_form to create a new user: it all works great except the acf/pre_save_post hook is apparently running twice, thus my user is created and then created again, erroneously returning ‘username already exists’ on the second pass. I’ve resolved this by removing the init filter in my plugin which adds acf_form_head. I did this knowing that my theme already adds it.

    However, I wonder if there’s a method for checking whether acf_form_head has already been added… my plugin should work independently of the theme so really I should be adding acf_form_head in both as it’s a dependency.

    Does anyone have a method for avoiding this possible duplication of acf_form_head?

  • You can try

    
    if (did_action('acf/submit_form')) == 0) {
      acf_form_head();
    }
    
  • Thank you, John. I’ve used your code and the duplication is no longer happening. However, I think that on this occasion the solution was to prevent it loading twice in the front and admin as described in this post: https://support.advancedcustomfields.com/forums/topic/acf_form-always-creates-two-posts/#post-53099

    Thanks again

  • ACF form head should not be run in the admin, normally.

    The action I referenced acf/submit_form is run during acf_form_head(). If this action has been done that means that acf_form_head() was already called somewhere else. There are other actions as well that could be used but this one seemed the most likely.

    I would also check the the other thing.

    
    if (!is_admin() && did_action('acf/submit_form')) == 0) {
      acf_form_head();
    }
    
  • Sweet, thanks for clarifying, John. That’s what I’m using. There’s a typo in your solution above (for the benefit of anyone finding and hoping this) in the form of an extra closing parentheses. Should be…

    
    if( ! is_admin() && did_action( 'acf/submit_form' ) == 0 ) {
      acf_form_head();
    }
    
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.