Add extra field to wishlist profile fields

To add extra fields to the wishlist profile fields, you can use this example which adds a phone field to the wishlist profile form.

STEP 1 (Optional): Register the phone field to the wishlist

This step is only necessary if you want the field to be saved automatically by the plugin. In this case, we want the field to be saved as wishlist meta data.

/**
 * Register the phone field as wishlist meta data
 */
function my_nmgr_register_phone_field( $data, $object ) {
		if ( is_a( $object, 'NMGR_Wishlist' ) ) {
			$data[ 'phone' ] = '';
		}
		return $data;
	}
add_filter( 'nmgr_get_meta_data', 'my_nmgr_register_phone_field', 10, 2 );

STEP 2: Add the phone field to the wishlist profile form

/**
 * Add the phone field to the array of fields for the plugin
 */
function my_nmgr_add_phone_field( $fields, $args ) {

	$fields[ 'phone' ] = array(
		'type' => 'text',
		'label' => __( 'Phone', 'my-text-domain' ), // change 'my-text-domain' to your text domain.
		'placeholder' => __( 'Phone', 'my-text-domain' ),
		'required' => true, // remove this or set it to false if the field is not required
		'fieldset' => 'profile', // this makes the field appear among the profile fields
		'value' => get_post_meta( $args[ 'wishlist' ]->get_id(), 'phone', true ),
	);

	return $fields;
}

add_filter( 'nmgr_fields', 'my_nmgr_add_phone_field', 10, 2 );

STEP 3 (Optional): Move the phone field above the email field

/**
 * Place the phone field above the email field
 */
function my_nmgr_set_phone_field_above_email( $fields ) {
	$phone_field = '';

	if ( isset( $fields[ 'phone' ], $fields[ 'email' ] ) ) {
		$phone_field = $fields[ 'phone' ];
		unset( $fields[ 'phone' ] );
	}

	if ( $phone_field ) {
		$email_field_index = array_search( 'email', array_keys( $fields ) );
		$fields = array_slice( $fields, 0, $email_field_index ) +
			array( 'phone' => $phone_field ) +
			array_slice( $fields, $email_field_index );
	}

	return $fields;
}

add_filter( 'nmgr_fields', 'my_nmgr_set_phone_field_above_email' );

STEP 4 (Optional): Save the phone field when the other fields are saved

This step is only necessary if you have not registered the field to the wishlist previously so that it can be saved automatically, but you instead want to save it manually maybe by adding custom logic before save.

/**
 * Save the phone field
 */
function my_nmgr_save_phone_field( $extra_data, $object ) {
	if ( is_a( $object, 'NMGR_Wishlist' ) && isset( $extra_data[ 'phone' ] ) ) {
		update_post_meta( $object->get_id(), 'phone', sanitize_text_field( $extra_data[ 'phone' ] ) );
	}
}

add_action( 'nmgr_save_extra_data', 'my_nmgr_save_phone_field', 10, 2 );