Support

Account

Home Forums Bug Reports Update user meta with ACF pro throw error when displaying data

Helping

Update user meta with ACF pro throw error when displaying data

  • I’m creating a theme in wordpress with latest version of ACF PRO and I’m get problems creating an user and update his user meta in a custom php file.

    I have a page in front that have a big form that contains basic user info and others input that I created in ACF.

    The first thing that I do is:

    
    <?php
    require_once("../../../../wp-load.php");
    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' );
    include_once( ABSPATH . 'wp-content/plugins/advanced-custom-fields-pro/acf.php');
    
    require_once( 'aux_functions.php' );
    
    if ( $_POST ) {
    
      $last_name              = $_POST['last_name'];
      $name                   = $_POST['name'];
      $email                  = $_POST['email'];
      $rut                    = $_POST['rut'];
      $telefono               = $_POST['telefono'];
      $profesion              = $_POST['profesion'];
      ... more vars...
    
      if ( !username_exists($pseudo) && !email_exists($email) ) {
    
        $random_password = wp_generate_password( $length = 8, false );
        $key_active      = wp_generate_password( $length = 20, false );
    
        $user_id         = wp_create_user( $pseudo, $random_password, $email );
        $user_id_role    = new WP_User($user_id);
        $user_id_role->set_role($tipo_de_agente);
    
        update_user_meta($user_id, 'pseudo', $pseudo );
        update_user_meta($user_id, 'key_new', $key_active );
    
        update_user_meta($user_id, 'first_name', $name );
        update_user_meta($user_id, 'last_name', $last_name );
    
        update_user_meta_if_exist($user_id, 'rut', $rut);
        update_user_meta_if_exist($user_id, 'telefono', $telefono);
        update_user_meta_if_exist($user_id, 'profesion', $profesion);
        update_user_meta_if_exist($user_id, 'region', $region);

    This is the function update_user_meta_if_exist:

    
    function update_user_meta_if_exist($user_id, $key, $meta_data) {
      if ( !empty($meta_data) ) {
        update_user_meta($user_id, $key, $meta_data );
      }
    }

    This save the data correctly. The problem is when I display the data in front.
    I selected option that selects and checkbox return an array of his value and label like this: ['label' => 'Label', 'value' => 'value']

    Aaannnddd… When I create the user with the function above. The selects and Checkbox inputs return only value instead of array

    The problems is solved when I go to the user administration in wordpress desk, select the user recently created and click the update user button. When I do that, the selects and checkbox return the array instead of only value

    Anyone can help?

  • When ACF adds data to the database it adds to entries in meta. The first is the meta key as you are inserting. The second is the ACF field key reference. The meta key for this field name prepended with an underscore "_{$field_name}" and the value of this is the field key of the field. Whithout this ACF does not know what to do with your field and is why it is not working and why it does work after you edit and update the user that was added.

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

You must be logged in to reply to this topic.