{"id":1193,"date":"2014-04-29T15:46:27","date_gmt":"2014-04-30T01:46:27","guid":{"rendered":"http:\/\/mymonkeydo.com\/?p=1193"},"modified":"2014-04-29T15:46:27","modified_gmt":"2014-04-30T01:46:27","slug":"wordpress-heartbeat-api","status":"publish","type":"post","link":"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/","title":{"rendered":"WordPress Heartbeat API"},"content":{"rendered":"

I could not find any\u00a0extensive documentation on the WordPress Heartbeat API, so I am writing here to remember what I have learned through various tutorials throughout the web.<\/p>\n

The Heartbeat API is written in Javascript. \u00a0Think of it like a javascript function that gets run every 60 seconds (depending on the interval) which sends data via POST to the server and then the server returns a response. \u00a0All of which you can easily hook into. \u00a0It was introduced in WordPress 3.6.<\/p>\n

Quick\u00a0Examples<\/h2>\n

PHP<\/strong><\/p>\n

\n\n\/\/ server received whatever parameters from when javascript triggered the "heartbeat-send" event.\nfunction myplugin_heartbeat_received( $response, $data ) {\nif ( !empty( $data['myplugin_id'] ) ) {\n$response['myplugin_got_it'] = true;\n}\nreturn $response;\n}\nadd_filter( 'heartbeat_received', 'myplugin_heartbeat_received', 10, 2 );\n\n<\/pre>\n

Javascript<\/strong><\/p>\n

\n\njQuery(document).ready(function($) {\nwp.heartbeat.interval= 15;\n\/\/ set some parameters when it's time to send data to the server\n$(document).on('heartbeat-send.myplugin', function(e, data){\ndata['myplugin_id'] =\u00a0my_id;\n});\n\n\/\/ do something when your page gets a response.\n$(document).on('heartbeat-tick.myplugin', function(e, data){\n\nif ( data.hasOwnProperty( 'myplugin_got_it' ) ) {\nrefreshPage();\n}\n});\n\n});\n\n<\/pre>\n

Heartbeat Pulse Interval<\/h2>\n

The ‘heartbeat_settings’ filter gets applied in the ‘init’ hook, priority 0 in\u00a0wp_default_scripts in wp-includes\/script-loader.php. \u00a0So if you do use the hook on the server-side and want to only run it on certain pages, be prepared to parse the URL because a lot of WP functions aren’t available at that time. \u00a0Alternatively you can set the interval in javascript with wp.heartbeat.interval(15) where 15 is seconds. \u00a0Note some other tutorials say to use ‘fast’, ‘standard’, ‘slow’, but that didn’t work for me for some reason.<\/p>\n

 <\/p>\n

Tutorials<\/h2>\n

I found these tutorials to be the most helpful.<\/p>\n

http:\/\/code.tutsplus.com\/tutorials\/the-heartbeat-api-getting-started–wp-32446<\/a><\/p>\n

http:\/\/code.tutsplus.com\/tutorials\/the-heartbeat-api-changing-the-pulse–wp-32462<\/a><\/p>\n

http:\/\/code.tutsplus.com\/tutorials\/heartbeat-api-using-heartbeat-in-a-plugin–wp-32496<\/a><\/p>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[2],"tags":[39,54,64],"yoast_head":"\nWordPress Heartbeat API - My Monkey Do<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WordPress Heartbeat API - My Monkey Do\" \/>\n<meta property=\"og:url\" content=\"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/\" \/>\n<meta property=\"og:site_name\" content=\"My Monkey Do\" \/>\n<meta property=\"article:published_time\" content=\"2014-04-30T01:46:27+00:00\" \/>\n<meta name=\"author\" content=\"Webhead\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Webhead\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/\",\"url\":\"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/\",\"name\":\"WordPress Heartbeat API - My Monkey Do\",\"isPartOf\":{\"@id\":\"https:\/\/mymonkeydo.com\/#website\"},\"datePublished\":\"2014-04-30T01:46:27+00:00\",\"dateModified\":\"2014-04-30T01:46:27+00:00\",\"author\":{\"@id\":\"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829\"},\"breadcrumb\":{\"@id\":\"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/mymonkeydo.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"WordPress Heartbeat API\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/mymonkeydo.com\/#website\",\"url\":\"https:\/\/mymonkeydo.com\/\",\"name\":\"My Monkey Do\",\"description\":\"A Log of Coding Solutions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/mymonkeydo.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829\",\"name\":\"Webhead\",\"url\":\"https:\/\/mymonkeydo.com\/author\/corey\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WordPress Heartbeat API - My Monkey Do","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/","og_locale":"en_US","og_type":"article","og_title":"WordPress Heartbeat API - My Monkey Do","og_url":"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/","og_site_name":"My Monkey Do","article_published_time":"2014-04-30T01:46:27+00:00","author":"Webhead","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Webhead","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/","url":"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/","name":"WordPress Heartbeat API - My Monkey Do","isPartOf":{"@id":"https:\/\/mymonkeydo.com\/#website"},"datePublished":"2014-04-30T01:46:27+00:00","dateModified":"2014-04-30T01:46:27+00:00","author":{"@id":"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829"},"breadcrumb":{"@id":"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/mymonkeydo.com\/wordpress-heartbeat-api\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/mymonkeydo.com\/"},{"@type":"ListItem","position":2,"name":"WordPress Heartbeat API"}]},{"@type":"WebSite","@id":"https:\/\/mymonkeydo.com\/#website","url":"https:\/\/mymonkeydo.com\/","name":"My Monkey Do","description":"A Log of Coding Solutions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/mymonkeydo.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/mymonkeydo.com\/#\/schema\/person\/b16fc650a8c182faaac896bab099b829","name":"Webhead","url":"https:\/\/mymonkeydo.com\/author\/corey\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts\/1193"}],"collection":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/comments?post=1193"}],"version-history":[{"count":0,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/posts\/1193\/revisions"}],"wp:attachment":[{"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/media?parent=1193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/categories?post=1193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mymonkeydo.com\/wp-json\/wp\/v2\/tags?post=1193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}