• Resolved pzh20

    (@pzh20)


    I have the following hook that should be call when my form is submitted, but I’ts called even when I’m not doing anything associated with it. Is this a wordpress issue or something wrong with my code?

       acf_log('Inside FAM - Complete Family Member Title and date fields (acf extended form)');
    add_action('acfe/form/submit_post/form=add_family_member', 'my_post_submit', 10, 4);

    function after_save_post( $form, $post_id ){
    $firstname = get_field('first_name',$post_id);
    $middlenames = get_field('middle_names',$post_id);
    $lastname = get_field('surname',$post_id);
    $dob = date("d/m/Y", strtotime(get_field('date_of_birth',$post_id)));
    $dod = date("d/m/Y", strtotime(get_field('date_of_death',$post_id)));
    if($dod) {
    $dod = '> ' .get_field('date_of_death',$post_id);
    } else {
    $dod = null;
    }
    $post_title = implode(' ',array_filter([$firstname,$middlenames,$lastname,'-',$dob,$dod])) ;
    acf_log('Inside FAM - ' . $post_title);
    }

    Sorry, I forgot to say that I never get log entries for anything other than the first call to acf_log()

    • This topic was modified 7 months, 3 weeks ago by pzh20.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    Two things:

    • Your log acf_log('Inside FAM - Complete Family ...') is outside of your hook callback, so it will be executed on every page load no matter what.
    • Your hook callback is supposed to be named my_post_submit, but your function is called after_save_post(). So these two are unrelated.

    If you need further assistance regarding hooks, please see this guide for some explanation. You can also check the WP documentation.

    A typical hook with log would be:

    add_action('my_hook', 'my_callback');
    function my_callback(){
    acf_log('my hook is executed!');
    }

    Hope it helps!

    Regards.

    Thread Starter pzh20

    (@pzh20)

    Konrad,

    Can you confirm something about how I should work when using ACFE vs just ACF hooks.

    Before I started using ACFE, I use the acf/save_post hook to manage several different aspects of validation and field manipulation across several field groups, using

    if( get_post_type( $post_id ) == ‘my_cpt’ ) {….
    }
    if( get_post_type( $post_id ) == ‘my_other_cpt’ ) {…. etc

    also, I had 2 versions of this snippet, one for ACF in the back-end and a similar version for Frondend Admin for ACF, using it’s version of the acf hook.

    Now with ACFE, I take it I should use your version for your frontend forms, but when trying to keep the ACF backend version it’s currently failing. Should I still follow this approach (and fix the backend version) or do you recommend another option?

    Many thanks in advance

    Pete

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    The acf/save_post will be triggered both in the back-end and the front-end, when using the native acf_form() and ACF Extended Forms. The hooks related to ACF Extended Form such as acfe/form/submit_form will be only executed on the front-end.

    If you need to enhance your acf/save_post hook to only target the back-end submission, you can use the is_admin() helper in your hook code. See documentation.

    Hope it helps!

    Regards.

    Thread Starter pzh20

    (@pzh20)

    Thanks for this, however, after adding if (is_admin()) { } to wrap the code in my acf/save_post, yor hook acfe/form/submit_post isn’t being called at all!

    Here’s my code;

    add_action('acfe/form/submit_post/form=add_family_member', 'front_end_after_save_post', 10, 4);

    function front_end_after_save_post( $form, $post_id ){
    acf_log('Inside FAM - Complete Family Member Title and date fields (acf extended form)');
    $firstname = get_field('first_name',$post_id);
    $middlenames = get_field('middle_names',$post_id);
    $lastname = get_field('surname',$post_id);
    $dob = date("d/m/Y", strtotime(get_field('date_of_birth',$post_id)));
    $dod = date("d/m/Y", strtotime(get_field('date_of_death',$post_id)));
    if($dod) {
    $dod = '> ' .get_field('date_of_death',$post_id);
    } else {
    $dod = null;
    }

    $post_title = implode(' ',array_filter([$firstname,$middlenames,$lastname,'-',$dob,$dod])) ;
    acf_log('Inside FAM - ' . $post_title);
    }

    Is it possibly something to do with the priority which is set to 5 on the acf/save_post?

    Regards

    Pete

    Thread Starter pzh20

    (@pzh20)

    Can you explain what is the difference between ‘acfe/form/submit_post/form=my_form’ and ‘acfe/form/submit_form/form=my_form’ please?

    Regards

    Pete

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hook called over and over’ is closed to new replies.