Skip to content

Instantly share code, notes, and snippets.

@imath
Created July 23, 2014 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imath/6580442d3f8ed0192c40 to your computer and use it in GitHub Desktop.
Save imath/6580442d3f8ed0192c40 to your computer and use it in GitHub Desktop.
Happy Birthday !
<?php
/**
* Il y a effectivement un problème avec l'utilisation de bp_member_profile_data()
* dans le Members loop @see https://buddypress.trac.wordpress.org/ticket/4891
*
* J'avais indiqué un moyen de contournement sur le forum de BuddyPress.org
* @see http://buddypress.org/support/topic/possible-bug-with-bp_member_profile_data-and-date-selector/
*
* Dans le cas présent, je pense qu'il est de toute façon plus judicieux d'utiliser xprofile_get_field_data
* tout en désactivant réactivant le filtre xprofile_filter_format_field_value_by_field_id pour disposer de la
* valeur telle qu'en db car plus facile à manipuler.
*/
// Le champ xprofile a pour nom 'Birthday'
function bpfr_is_birthday( $user_id = 0, $field_name = 'Birthday' ) {
$return = false;
if ( empty( $user_id ) || empty( $field_name ) || ! bp_is_active( 'xprofile' ) ) {
return $return;
}
// Neutralisation du filtre pour garder le format tel qu'en base de données
remove_filter( 'xprofile_get_field_data', 'xprofile_filter_format_field_value_by_field_id', 5, 2 );
$birthday = xprofile_get_field_data( 'Birthday', $user_id );
// Restoration du filtre
add_filter( 'xprofile_get_field_data', 'xprofile_filter_format_field_value_by_field_id', 5, 2 );
$monthday_birthday = mysql2date( 'md', $birthday );
if ( $monthday_birthday === (string) date('md') ) {
$return = __( 'Happy Birthday!', 'plugin-domain' );
}
return $return;
}
// Members Directory loop & User's profile header
function bpfr_happy_birthday_wish() {
// Un profil est affiché > do_action( 'bp_before_member_header_meta' )
if ( bp_is_user() ) {
$user_id = bp_displayed_user_id();
// L'autre possibilité est nécessairement le directory loop > do_action( 'bp_directory_members_item' )
} else {
$user_id = bp_get_member_user_id();
}
// As-t-on un gagnant ?
$happy_birthday = bpfr_is_birthday( $user_id, 'Birthday' );
if ( ! empty( $happy_birthday ) ) {
echo $happy_birthday;
}
}
add_action( 'bp_directory_members_item', 'bpfr_happy_birthday_wish' );
add_action( 'bp_before_member_header_meta', 'bpfr_happy_birthday_wish' );
@solhuebner
Copy link

Good one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment