• MrConn

    (@mrconn)


    Hi,
    How can I display real-time post-updates on a page?

    What I would like to achieve is a live-counter that displays new/updated posts in a “counter-bubble”. Like on an online mail-client, that shows number of new mails in a folder your not looking in.

    Im thinking Heartbeat API, but I dont know how…?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I’ve never used heartbeat, documentation seems incomplete. It doesn’t appear to deliver much advantage over a conventional Ajax approach, which is the way I’ve accomplished similar needs.

    Your JS/jQuery code establishes a timer event with setInterval(). The related callback makes an Ajax query to the server to fetch the latest count. It might also send some criteria about what count you’re looking for, like a date to count from.

    Server side, your Ajax handler makes an appropriate count query and returns the value to the client browser. Your JS/jQuery code receives this value and updates the DOM element that contains the count the user sees.

    If you’ve not coded Ajax for WP before, it’s not that different from any other Ajax technique used on any webpage, but there are a few WP specific quirks. Mainly that an “action” value must be sent with the request and the request must be sent to /wp-admin/admin-ajax.php. Your Ajax handler code must be hooked to an action whose handle is partly composed from the passed “action” value. More details here:
    https://developer.wordpress.org/plugins/javascript/ajax/

Viewing 1 replies (of 1 total)

The topic ‘Live new-post alert’ is closed to new replies.