Support

Account

Home Forums Front-end Issues profile_update hook and acf frontend form

Solved

profile_update hook and acf frontend form

  • Hello,

    i’ve an question: if i use the profile_update hook in combination with acf, the fields updating on the backend, but not in the frontend form. Any ideas?

    WordPress Only

    function custom_user_update( $user_id, $previous_data ) 
    {				
    	update_user_meta( $user_id, 'network_public', false);	
    }
    add_action( 'profile_update', 'custom_user_update', 10, 2  );

    Any ideas how i can use this in a frontend form (with user_$id?)? Just test it out with acf/save_post, but is not working for me.

    AND an simple other question (for sure): How can i check, if a field exists(!)? Like “has_field” or some else…

    Sorry for my bad english!

    Greetings.

  • acf/save_posts is run on any page where custom field are shown and only when there are acf fields shown and submitted.

    If acf/save_post is not running then there are not acf fields being submitted.

    when ACF passes the $post_id value to your filter it will be in the form

    
    "user_{$user_id}"
    

    in order to use standard WP function you will need to extract the user ID

    
    function whatever_your_function_name_is($post_id) {
      // see if the post ID is for a user
      if (strpos($post_id, 'user_') !== 0) {
        // string does not start with user_
        // bail
        return;
      }
      $user_id = intval(substr($post_id, 5));
      // your code to update user here
    }
    
  • So if i use (is it correct?) it should work?

    function 123_update_user( $post_id ) 
    {			
      if (strpos($post_id, 'user_') !== 0)
    		return;
    	
      $user_id = intval(substr($post_id, 5));		
    	
    	update_user_meta( $user_id, 'network_public', false);	
    }
    add_action( 'acf/save_post', '123_update_user', 5  );
  • Still not working. I use acf_form() to update a user profile in frontend…

    acf_form_head() is set and my form looks like:

    $groups = array( 1, 2, 3, 4 );

    acf_form(array(
      'post_id' => 'user_' . get_current_user_id(),
      'field_groups' => $groups,
    ));

    Fields are saving perfect, but field “network_public” does not update. field network_public is an field in a field_group in the acf_form.

    The script for set network_public to false, is the same as above (posted recently from me).

  • Add the following to the top of your function to test

    
    die('My filter is running');
    

    try to save the page and see if it appears. Remove the code as soon as it does.

    Possible corrective actions

    1) Set the priority for your action > 10

    2) Make sure you followed all of the steps for using front end forms https://www.advancedcustomfields.com/resources/acf_form/
    — did you include acf_form_head() before get_header()

    check your theme, make sure it includes both wp_head() and wp_footer() calls

  • 1) Set the priority for your action > 10

    This helped me! 🙂 But now i cant check, if a field is updated or not, or?

  • so the problem was that ACF was making changes to the field after you did.

    If you need to look for updates then you may need to have 2 actions, one that runs before ACF updates and one that runs after ACF updates.

    The other alternative is, instead of updating the post meta, to alter $_POST['acf']['YOUR FIELD KEY'] with the value you want for "network_public"

  • You’re awesome John! Thank you! Work like a charm!

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

The topic ‘profile_update hook and acf frontend form’ is closed to new replies.