bp_word_or_name( string $youtext, string $nametext, bool $capitalize = true, bool $echo = true )
Select between two dynamic strings, according to context.
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 Connections" and "Joe’s Connections"). 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
- $youtext
-
(Required) The "you" version of the phrase (eg "Your Connections").
- $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 Connections").
- $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
Return
(string|null) $nametext If ! $echo, returns the appropriate string.
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 BuddyPress 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 ); } } }
Changelog
Version | Description |
---|---|
BuddyPress 1.0.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.