1. Docs
  2. Developer Guide
  3. Miscellaneous and Snippets
  4. Add Address Fields To Registration Form

Add Address Fields To Registration Form

add_filter('propertyhive_user_details_form_fields', 'add_address_fields');
function add_address_fields( $fields )
{
    if ( is_user_logged_in() )
    {
        $current_user = wp_get_current_user();

        if ( $current_user instanceof WP_User )
        {
            $contact = new PH_Contact( '', $current_user->ID );
        }
    }

    $fields['address_name_number'] = array(
        'type' => 'text',
        'label' => __( 'House Name / Number', 'propertyhive' ),
        'required' => false
    );
    if ( is_user_logged_in() && $current_user instanceof WP_User )
    {
        $fields['address_name_number']['value'] = $current_user->address_name_number;
    }

    $fields['address_street'] = array(
        'type' => 'text',
        'label' => __( 'Street', 'propertyhive' ),
        'required' => false
    );
    if ( is_user_logged_in() && $current_user instanceof WP_User )
    {
        $fields['address_street']['value'] = $current_user->address_street;
    }

    $fields['address_two'] = array(
        'type' => 'text',
        'label' => __( 'Address Line 2', 'propertyhive' ),
        'required' => false
    );
    if ( is_user_logged_in() && $current_user instanceof WP_User )
    {
        $fields['address_two']['value'] = $current_user->address_two;
    }

    $fields['address_three'] = array(
        'type' => 'text',
        'label' => __( 'Town / City', 'propertyhive' ),
        'required' => false
    );
    if ( is_user_logged_in() && $current_user instanceof WP_User )
    {
        $fields['address_three']['value'] = $current_user->address_three;
    }

    $fields['address_four'] = array(
        'type' => 'text',
        'label' => __( 'County', 'propertyhive' ),
        'required' => false
    );
    if ( is_user_logged_in() && $current_user instanceof WP_User )
    {
        $fields['address_four']['value'] = $current_user->address_four;
    }

    $fields['address_postcode'] = array(
        'type' => 'text',
        'label' => __( 'Postcode', 'propertyhive' ),
        'required' => false
    );
    if ( is_user_logged_in() && $current_user instanceof WP_User )
    {
        $fields['address_postcode']['value'] = $current_user->address_postcode;
    }

    return $fields;
}

add_action( 'propertyhive_applicant_registered', 'save_address_fields' );
function save_address_fields( $contact_post_id, $user_id )
{
    update_post_meta( $contact_post_id, '_address_name_number', sanitize_text_field($_POST['address_name_number']) );
    update_post_meta( $contact_post_id, '_address_street', sanitize_text_field($_POST['address_street']) );
    update_post_meta( $contact_post_id, '_address_two', sanitize_text_field($_POST['address_two']) );
    update_post_meta( $contact_post_id, '_address_three', sanitize_text_field($_POST['address_three']) );
    update_post_meta( $contact_post_id, '_address_four', sanitize_text_field($_POST['address_four']) );
    update_post_meta( $contact_post_id, '_address_postcode', sanitize_text_field($_POST['address_postcode']) );
}
Was this article helpful to you? Yes No