bp_core_replace_tokens_in_text

Replace all tokens in the input text with appropriate values.

Description

(string) bp_core_replace_tokens_in_text( (string) $text, (array) $tokens ); 

Intended for use with the email system introduced in BuddyPress 2.5.0.

Returns (string)

Parameters (2)

0. $text (string)
Text to replace tokens in.
1. $tokens (array)
Token names and replacement values for the $text.

Usage

  1. if ( !function_exists( 'bp_core_replace_tokens_in_text' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-functions.php'; 
  3.  
  4. // Text to replace tokens in. 
  5. $text = ''; 
  6.  
  7. // Token names and replacement values for the $text. 
  8. $tokens = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_core_replace_tokens_in_text($text, $tokens); 
  12.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-functions.php  
  1. function bp_core_replace_tokens_in_text( $text, $tokens ) { 
  2. $unescaped = array(); 
  3. $escaped = array(); 
  4.  
  5. foreach ( $tokens as $token => $value ) { 
  6. if ( ! is_string( $value ) && is_callable( $value ) ) { 
  7. $value = call_user_func( $value ); 
  8.  
  9. // Tokens could be objects or arrays. 
  10. if ( ! is_scalar( $value ) ) { 
  11. continue; 
  12.  
  13. $unescaped[ '{{{' . $token . '}}}' ] = $value; 
  14. $escaped[ '{{' . $token . '}}' ] = esc_html( $value ); 
  15.  
  16. $text = strtr( $text, $unescaped ); // Do first. 
  17. $text = strtr( $text, $escaped ); 
  18.  
  19. /** 
  20. * Filters text that has had tokens replaced. 
  21. * @since 2.5.0 
  22. * @param string $text 
  23. * @param array $tokens Token names and replacement values for the $text. 
  24. */ 
  25. return apply_filters( 'bp_core_replace_tokens_in_text', $text, $tokens );