Skip to content

Instantly share code, notes, and snippets.

@imath
Last active May 30, 2023 19:53
Show Gist options
  • Save imath/b75af59a6b70e30d9b3a013ab0814448 to your computer and use it in GitHub Desktop.
Save imath/b75af59a6b70e30d9b3a013ab0814448 to your computer and use it in GitHub Desktop.
Use an alternative to `bp_core_get_user_displayname()` to get the user's display name without HTML tags when sending a friendship request.
<?php
/**
* Custom function file.
*
* Put a file called `bp-custom.php` into your /wp-content/plugins folder.
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Uses the WP User display name to avoid filters in `bp_core_get_user_displayname()`
*
* @since 1.0.0
*
* @param array $args Email tokens.
* @return array Email tokens.
*/
function nohtmltag_in_friends_email_subject( $args = array() ) {
if ( isset( $args['tokens']['friend-requests.url'], $args['tokens']['initiator.name'], $args['tokens']['initiator.id'] ) && $args['tokens']['initiator.id'] ) {
$user = get_user_by( 'id', $args['tokens']['initiator.id'] );
if ( isset( $user->ID ) ) {
/*
* Edit the title of the Email from the WP Admin > Emails screen replacing
* [{{{site.name}}}] New friendship request from {{initiator.name}} with:
* [{{{site.name}}}] New friendship request from {{initiator.first_and_last_name}}
*
* That being said, I sugget to always use `$user->display_name` instead of
* `$user->first_name . ' ' . $user->last_name` as some users are not setting these fields
* and BuddyPress guesses these from the display name using what's before the space as the first
* name & what's after the space as the last name.
*/
$args['tokens']['initiator.first_and_last_name'] = $user->first_name . ' ' . $user->last_name;
}
}
return $args;
}
add_filter( 'bp_before_send_email_parse_args', 'nohtmltag_in_friends_email_subject', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment