bp_word_or_name( string $youtext, string $nametext, bool $capitalize = true, bool $echo = true )

Select between two dynamic strings, according to context.


Description Description

This function can be used in cases where a phrase used in a template will differ for a user looking at his own profile and a user looking at another user’s profile (eg, "My Friends" and "Joe’s Friends"). Pass both versions of the phrase, and bp_word_or_name() will detect which is appropriate, and do the necessary argument swapping for dynamic phrases.


Parameters Parameters

$youtext

(Required) The "you" version of the phrase (eg "Your Friends").

$nametext

(Required) The other-user version of the phrase. Should be in a format appropriate for sprintf() - use %s in place of the displayed user's name (eg "%'s Friends").

$capitalize

(Optional) Force into title case. Default: true.

Default value: true

$echo

(Optional) True to echo the results, false to return them. Default: true.

Default value: true


Top ↑

Return Return

(string|null) $nametext If ! $echo, returns the appropriate string.


Top ↑

Source Source

File: bp-core/bp-core-template.php

function bp_word_or_name( $youtext, $nametext, $capitalize = true, $echo = true ) {

	if ( ! empty( $capitalize ) ) {
		$youtext = bp_core_ucfirst( $youtext );
	}

	if ( bp_displayed_user_id() == bp_loggedin_user_id() ) {
		if ( true == $echo ) {

			/**
			 * Filters the text used based on context of own profile or someone else's profile.
			 *
			 * @since 1.0.0
			 *
			 * @param string $youtext Context-determined string to display.
			 */
			echo apply_filters( 'bp_word_or_name', $youtext );
		} else {

			/** This filter is documented in bp-core/bp-core-template.php */
			return apply_filters( 'bp_word_or_name', $youtext );
		}
	} else {
		$fullname = bp_get_displayed_user_fullname();
		$fullname = (array) explode( ' ', $fullname );
		$nametext = sprintf( $nametext, $fullname[0] );
		if ( true == $echo ) {

			/** This filter is documented in bp-core/bp-core-template.php */
			echo apply_filters( 'bp_word_or_name', $nametext );
		} else {

			/** This filter is documented in bp-core/bp-core-template.php */
			return apply_filters( 'bp_word_or_name', $nametext );
		}
	}
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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