xprofile_filter_link_profile_data( string $field_value, string $field_type = 'textbox' )

Filter an Extended Profile field value, and attempt to make clickable links to members search results out of them.


Description Description

  • Not run on datebox field types.
  • Not run on values without commas with less than 5 words.
  • URL’s are made clickable.

To disable globally: remove_filter( ‘bp_get_the_profile_field_value’, ‘xprofile_filter_link_profile_data’, 9, 3 );

To disable for a single field, use the ‘Autolink’ settings in Dashboard > Users > Profile Fields.


Top ↑

Parameters Parameters

$field_value

(string) (Required) Profile field data value.

$field_type

(string) (Optional) Profile field type.

Default value: 'textbox'


Top ↑

Return Return

(string|array)


Source Source

File: bp-xprofile/bp-xprofile-filters.php

function xprofile_filter_link_profile_data( $field_value, $field_type = 'textbox' ) {
	global $field;

	if ( ! $field->get_do_autolink() ) {
		return $field_value;
	}

	if ( 'datebox' === $field_type ) {
		return $field_value;
	}

	if ( strpos( $field_value, ',' ) === false && strpos( $field_value, ';' ) === false && ( count( explode( ' ', $field_value ) ) > 5 ) ) {
		return $field_value;
	}

	if ( strpos( $field_value, ',' ) !== false ) {
		// Comma-separated lists.
		$list_type = 'comma';
		$values    = explode( ',', $field_value );
	} else {
		/*
		 * Semicolon-separated lists.
		 *
		 * bp_xprofile_escape_field_data() runs before this function, which often runs esc_html().
		 * In turn, that encodes HTML entities in the string (";" becomes "'").
		 *
		 * Before splitting on the ";" character, decode the HTML entities, and re-encode after.
		 * This prevents input like "O'Hara" rendering as "O' Hara" (with each of those parts
		 * having a separate HTML link).
		 */
		$list_type   = 'semicolon';
		$field_value = wp_specialchars_decode( $field_value, ENT_QUOTES );
		$values      = explode( ';', $field_value );

		array_walk( $values, function( &$value, $key ) use ( $field_type, $field ) {
			$value = bp_xprofile_escape_field_data( $value, $field_type, $field->id );
		} );
	}

	if ( ! empty( $values ) ) {
		foreach ( (array) $values as $value ) {
			$value = trim( $value );

			// If the value is a URL, skip it and just make it clickable.
			if ( preg_match( '@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $value ) ) {
				$new_values[] = make_clickable( $value );

			// Is not clickable.
			} else {

				// More than 5 spaces.
				if ( count( explode( ' ', $value ) ) > 5 ) {
					$new_values[] = $value;

				// Less than 5 spaces.
				} else {
					$query_arg    = bp_core_get_component_search_query_arg( 'members' );
					$search_url   = add_query_arg( array( $query_arg => urlencode( $value ) ), bp_get_members_directory_permalink() );
					$new_values[] = '<a href="' . esc_url( $search_url ) . '" rel="nofollow">' . $value . '</a>';
				}
			}
		}

		if ( 'comma' === $list_type ) {
			$values = implode( ', ', $new_values );
		} else {
			$values = implode( '; ', $new_values );
		}
	}

	return $values;
}


Top ↑

Changelog Changelog

Changelog
Version Description
1.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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