I wanted to utilize the WP 3.6 heartbeat API in an app I’m working on. I also wanted to include this feature before WordPress 3.6 officially shipped, so that meant it had to be compatible with WordPress 3.5.x as well.
Turns out, it’s really easy. The heartbeat API looks excellent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // enqueue the script, | |
| // this fails gracefully if no 'heartbeat' script is registered | |
| wp_enqueue_script('heartbeat'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function($) { | |
| // watch for heartbeat event, | |
| // no event will happen if the script isn't included | |
| $(document).on('heartbeat-connection-lost', function() { | |
| // do what you need to do here | |
| $('body').addClass('connection-lost') | |
| }).on('heartbeat-connection-restored', function() { | |
| // do what you need to do here | |
| $('body').removeClass('connection-lost') | |
| }); | |
| })(jQuery); |
Alex King: Backward Compatible WP Heartbeat API Usage: I wanted to utilize the WP 3.6 heartbeat API in an app … http://t.co/wz6JmY57mV
Alex King: Backward Compatible WP Heartbeat API Usage http://t.co/fOC9NizEYh
Alex King: Backward Compatible WP Heartbeat API Usage http://t.co/yWJ0hlRbNJ #wordpress
WordPress blog » Alex King: Backward Compatible WP Heartbeat API Usage http://t.co/eAjhZCebHv #WP
#Wordpress Alex King: Backward Compatible WP Heartbeat API Usage http://t.co/3V0iXQlpJ4
New on @alexkingorg: Backward Compatible WP Heartbeat API Usage http://t.co/o1gJ3ir4fQ
Alex King: Backward Compatible WP Heartbeat API Usage http://t.co/G7LJImBvWM #wordpress #wp
Backward Compatible WP Heartbeat API Usage http://t.co/RWJ27qER1J
[…] Heartbeat Backward Compatible […]
[…] Heartbeat Backward Compatible […]