Skip to:
Content
Pages
Categories
Search
Top
Bottom

New Message Ajax Notifiction

  • i am working on vary simple logic to implement a Ajax Notification as new message arrive.
    i know there is plugin available and i have also tried some but its not working, i dont know whay and then i decide to write my own code for that.

    i have added following code in Header.php of my theme

    
    function getMessages(){
    $('#user-messages span').load('ajax_messages.php');
    }
    setInterval( "getMessages()", 10000);
    

    and in ajax_messages.php i have code

    and i have added following code in Widgets using PHP code Widget.

    <a id="user-messages">
    <span></span>
    </a>

    according to me it should work fine but its not. i am getting error

    $ is not a function

    I am new to WordPress and i don’t know where i am wrong.

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Javascript is probably loading before jQuery is loading, thus the $ object isn’t set.

    Thanks for replying
    then what is solution for that ?
    i mean i want to know does i am following the correct path or wrong?

    Thanks

    any help


    Bowe
    Participant

    @bowromir

    If you’re using the WP bundled version of jQuery replace your $ with jQuery.. So:

    `

    function getMessages(){
    jQuery(‘#user-messages span’).load(‘ajax_messages.php’);
    }
    setInterval( “getMessages()”, 10000);

    `

    That should do it!

    Thanks and it worked.
    but its only show unread count on page load, but if user receive any message this did’t update.
    the main purpose of this script is to display total number of unread messages and it should update via ajax means if user receive any message, it should show total total number of unread messages without reloading page.

    i have bellow code in function.php of my theme

    `<?php
    function addMessageRefresh()
    {
    ?>

    function getMessages(){
    jQuery(‘#user-messages span’).text(“Unread Messages: ()”);
    }
    setInterval(“getMessages()”, 10000);

    <?php
    }
    add_action( ‘wp_head’, ‘addMessageRefresh’);
    ?>`

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘New Message Ajax Notifiction’ is closed to new replies.
Skip to toolbar