Skip to:

calling messages_get_unread_count() in new php file

  • i am trying to build one Buddy Press Theme and i have following code in my function.php

    function getMessages(){
    url: ‘message.php’,
    dataType: ‘html’,
    success: function (data) {
    jQuery(‘#user-messages span’).text(“Unread Messages: ” + data);

    and following code in message.php

    echo messages_get_unread_count();

    but when code execute i got following error

    `Unread Messages:
    Fatal error: Call to undefined function messages_get_unread_count() in url.php on line 1

    how to solve this issue and make function available in message.php


Viewing 2 replies - 1 through 2 (of 2 total)

  • Boone Gorges


    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’,

    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: Use `wp_ajax_` plus your chosen action name to define the hook:

    `function bbg_messages_ajax_callback() {
    echo messages_get_unread_count();
    add_action( ‘wp_ajax_get_message_count’, ‘bbg_messages_ajax_callback’ );`

    Boone Gorges


    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!

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.
Skip to toolbar