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:
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:
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:
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!
Viewing 2 replies - 1 through 2 (of 2 total)
The topic ‘calling messages_get_unread_count() in new php file’ is closed to new replies.