The problem is that when you load message.php via AJAX, you’re not loading all of WordPress and BuddyPress, which means that the messages_get_unread_count() function is not defined.
Instead of using ‘message.php’ as your AJAX url, use the one that BP provides for you, and be sure to provide an ‘action’ parameter:
` jQuery.ajax({
url: ajaxurl,
action: ‘get_message_count’,
dataType….`
That’ll ensure that WP/BP is loaded on your AJAX call.
Then, you’ll need to make sure that you have a listener function in your functions.php, using the `wp_ajax_` syntax. (Read more here: http://w4dev.com/wp/wp_ajax/) Use `wp_ajax_` plus your chosen action name to define the hook:
`function bbg_messages_ajax_callback() {
echo messages_get_unread_count();
die();
}
add_action( ‘wp_ajax_get_message_count’, ‘bbg_messages_ajax_callback’ );`
Finally, this code probably won’t work all by itself, because `messages_get_unread_count()` assumes a logged-in user – but on an AJAX request, you’re not guaranteed to have that. So you may want to amend your ajax call and callback like so, to manually provide a user_id:
` jQuery.ajax({
….
user_id: ”,
….`
Check out the way that bp-default does AJAX (buddypress/bp-themes/bp-default/_inc – the files ajax.php (for the PHP callbacks) and global.js (for the scripts). Good luck!