Using TDLC Birthdays to show the age in members list and profiles 2

This is an article about my TDLC Birthdays Buddypress plugin.

I got a couple of requests on how to reuse the TDLC Birthday PHP functions directly in the theme, on members profile pages. I don’t plan to develop an exhaustive API for these functions (what they do is too basic for that) but here is a quick and dirty snippet which does work with the current version of the plugin :)

Put the code below in the members-loop.php file (inside the main while loop, for example after the line <a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a>).

Note: it is highly recommended to use a Child Theme to avoid breaking updates and maintenance of your standard theme. For more information see this dedicated bage in the BuddyPress codex.

<?php
	$birthdatefield = '503';
	global $bp, $members_template;
	$userid =  $members_template->member->id;
	$birthdate = xprofile_get_field_data($birthdatefield, $userid);
	if(!is_numeric($birthdate)) $birthdate = strtotime($birthdate);
	if($birthdate) {
		$buddy = new BuddyWithABirthdate($userid, $birthdate);
		$age = $buddy->getAge();
		if(!$buddy->isBirthdayToday()) $age--;
		echo $age;
	}
?>

This snippet simply prints out the age of each member.

A few remarks :

  • $birthdatefield is the id or exact name of your custom birthdate field, don’t forget to update this value with your own. Of course it would be much cleaner not to hardcode it there – but hey, I told you this was quick & dirty ;-)
  • the plugin uses a timestamp integer as birthdate, in BP 1.5+ the stored object is a string so we need to convert it, hence the line if(!is_numeric($birthdate)) $birthdate = strtotime($birthdate);
  • the line if(!$buddy->isBirthdayToday()) $age--; is because getAge() actually returns the age the user will turn on their next birthday (except if the birthday is today).

Here is a version of the same snippet for the member-header.php (no loop, no need to get the user id here)

<?php
	$birthdatefield = '503';
	global $bp;
	$birthdate = xprofile_get_field_data($birthdatefield);
	if(!is_numeric($birthdate)) $birthdate = strtotime($birthdate);
	if($birthdate) {
		$buddy = new BuddyWithABirthdate(0, $birthdate);
		$age = $buddy->getAge();
		if(!$buddy->isBirthdayToday()) $age--;
		echo $age;
	}
?>

Have fun !

2 thoughts on “Using TDLC Birthdays to show the age in members list and profiles

  1. Reply crepmaster jan 10,2013 19:03

    Salut, j’ai un soucis avec la version la 0.4 sur wordpress 3.5

    j’obtiens cette erreur:
    WordPress database error: [Unknown column 'spam' in 'where clause']
    SELECT ID FROM wp_users WHERE ID=1 AND spam != 1
    Existe t il un fix?
    Merci.

  2. Reply Tom jan 25,2013 05:06

    Salut crepmaster,

    commente ou supprime la ligne 133 du fichier tdlc-birthdays/core.php : //$spam_column_test = $wpdb->get_row("SELECT ID FROM $wpdb->users WHERE ID=1 AND spam != 1");

    Ça devrait résoudre ton problème.

Leave a Reply