BP_XProfile_Field_Type_URL::pre_validate_filter( string $submitted_value = '', string|int $field_id = '' )

Modify submitted URL values before validation.


Description Description

The URL validation regex requires a http(s) protocol, so that all values saved in the database are fully-formed URLs. However, we still want to allow users to enter URLs without a protocol, for a better user experience. So we catch submitted URL values, and if the protocol is missing, we prepend ‘http://’ before passing to is_valid().


Top ↑

Parameters Parameters

$submitted_value

(string) (Optional) Raw value submitted by the user.

Default value: ''

$field_id

(string|int) (Optional) ID of the field.

Default value: ''


Top ↑

Return Return

(string)


Source Source

File: bp-xprofile/classes/class-bp-xprofile-field-type-url.php

	public static function pre_validate_filter( $submitted_value = '', $field_id = '' ) {

		// Allow empty URL values.
		if ( empty( $submitted_value ) ) {
			return '';
		}

		// Run some checks on the submitted value.
		if ( false === strpos( $submitted_value, ':'  ) &&
			substr( $submitted_value, 0, 1 ) !== '/' &&
			substr( $submitted_value, 0, 1 ) !== '#' &&
			! preg_match( '/^[a-z0-9-]+?\.php/i', $submitted_value )
		) {
			$submitted_value = 'http://' . $submitted_value;
		}

		return $submitted_value;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.4.0 Added the $field_id parameter.
2.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.