Support

Account

Home Forums ACF PRO Keep user Description meta value synched to ACF field

Helping

Keep user Description meta value synched to ACF field

  • Hi,
    I’m trying to replace the built-in user’s Description field by an ACF Wysiwyg Editor, but failing miserably. When I save the user edit form, the description field shows a different value than the ACF Field.

    This is what i thought would work

    
    // SYNC USER META DATA
    add_filter('acf/update_value/name=user_bio', 'acf_set_user_meta_description', 20, 3);
    
    // FUNCTIONS
    
    function acf_set_user_meta_description( $value, $post_id, $field ){
    
    	$updated = update_user_meta( $post_id, 'description', $value );
    
    	// So check and make sure the stored value matches $new_value.
    
    	if ( $updated !== get_user_meta( $post_id,  'description', true ) ) {
    		wp_die( __( 'The User Description field was incorrectly, or not at all, updated.', 'textdomain' ) );
    	}
    
    	return $value;
    }
  • Actually found a solution, using the save_post filter.

    
    
    // SYNC USER META DATA
    add_filter('acf/save_post', 'acf_set_user_meta_description', 20, 1);
    
    // FUNCTIONS
    function acf_set_user_meta_description( $user_id ){
    
    	// Keep User meta 'description' in sync with our ACF field
    	$bio = get_field('user_bio', $user_id);
    	
    	// $user_id is actually user_{id}. To update WP, we remove the "user_" part.
    	$user_id = (int)substr(strrchr($user_id, "_"), 1);
    	
    	if(!empty($user_id)){
    	// Will return false if the previous value is the same as $new_value.
    		$updated = update_user_meta( $user_id, 'description', $bio );
    	}
    }
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Keep user Description meta value synched to ACF field’ is closed to new replies.