{"id":10914,"date":"2021-04-21T07:17:08","date_gmt":"2021-04-21T07:17:08","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/?post_type=code_snippets&#038;p=10914"},"modified":"2026-10-10T19:53:56","modified_gmt":"2026-10-10T19:53:56","slug":"modify-or-disable-heartbeat-api-without-plugin","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/modify-or-disable-heartbeat-api-without-plugin\/","title":{"rendered":"Slow Down or Disable the WordPress Heartbeat API"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The WordPress Heartbeat API keeps the browser and the server in touch: every 15 to 60 seconds an open admin tab sends a request to <code>admin-ajax.php<\/code>. It powers post locking, the &#8220;session expired&#8221; login popup and live notices from plugins. On shared hosting, a few editors with many open tabs add up to a lot of PHP requests, and some hosts throttle or suspend accounts for it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet modifies the Heartbeat API without a plugin: it slows it down everywhere, switches it off on the front end, and can also switch it off in wp-admin while keeping it on the editor screens where it matters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Slow down the WordPress Heartbeat API and switch it off where it is not needed.\n *\/\n\n\/\/ ===== Config =====\nfunction dpc_heartbeat_config(): array {\n\t$config = array(\n\t\t\/\/ Seconds between Heartbeat requests, 15-120. WordPress default: 60.\n\t\t'interval'      =&gt; 120,\n\t\t\/\/ Stop Heartbeat on the front end (only if no other script there depends on it).\n\t\t'disable_front' =&gt; true,\n\t\t\/\/ Stop Heartbeat in wp-admin, except on the post editor screens.\n\t\t\/\/ This also turns off the \"Your session has expired\" login popup on those screens.\n\t\t'disable_admin' =&gt; false,\n\t);\n\t\/\/ ===== End of config =====\n\n\treturn (array) apply_filters( 'dpc_heartbeat_config', $config );\n}\n\nadd_filter( 'heartbeat_settings', 'dpc_heartbeat_interval' );\n\nfunction dpc_heartbeat_interval( $settings ) {\n\t$config = dpc_heartbeat_config();\n\n\tif ( ! empty( $config&#91;'interval'&#93; ) ) {\n\t\t$settings&#91;'interval'&#93; = max( 15, min( 120, (int) $config&#91;'interval'&#93; ) );\n\t}\n\n\treturn $settings;\n}\n\nadd_action( 'wp_enqueue_scripts', 'dpc_heartbeat_front', PHP_INT_MAX );\n\nfunction dpc_heartbeat_front(): void {\n\t$config = dpc_heartbeat_config();\n\n\tif ( ! empty( $config&#91;'disable_front'&#93; ) ) {\n\t\t\/\/ Dequeue, not deregister: scripts that depend on Heartbeat still get it.\n\t\twp_dequeue_script( 'heartbeat' );\n\t}\n}\n\n\/\/ wp-admin loads Heartbeat through the session-expired check (wp-auth-check) on every screen.\nadd_filter( 'wp_auth_check_load', 'dpc_heartbeat_admin', 10, 2 );\n\nfunction dpc_heartbeat_admin( $show, $screen = null ) {\n\t$config = dpc_heartbeat_config();\n\n\tif ( empty( $config&#91;'disable_admin'&#93; ) || ! $screen instanceof WP_Screen ) {\n\t\treturn $show;\n\t}\n\n\t\/\/ Keep it on the editor: post locks and the block editor need it.\n\treturn 'post' === $screen-&gt;base ? $show : false;\n}\n\nadd_action( 'admin_enqueue_scripts', 'dpc_heartbeat_admin_dequeue', PHP_INT_MAX );\n\nfunction dpc_heartbeat_admin_dequeue(): void {\n\t$config = dpc_heartbeat_config();\n\t$screen = get_current_screen();\n\n\tif ( ! empty( $config&#91;'disable_admin'&#93; ) &amp;&amp; $screen &amp;&amp; 'post' !== $screen-&gt;base ) {\n\t\twp_dequeue_script( 'heartbeat' );\n\t}\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>heartbeat_settings<\/code> sets the options WordPress prints for <code>heartbeat.js<\/code>. The snippet sets <code>interval<\/code> to your value, limited to 15\u2013120 seconds. Post locks expire after 150 seconds, so a longer interval would let two people edit the same post.<\/li>\n\n\n<li>On the front end the snippet dequeues <code>heartbeat<\/code> at the very end of <code>wp_enqueue_scripts<\/code>. It does not deregister it: a plugin script that lists Heartbeat as a dependency still loads it and keeps working.<\/li>\n\n\n<li>In wp-admin, Heartbeat is loaded on every screen by the session-expired check, <code>wp-auth-check<\/code>. With <code>disable_admin<\/code> on, the <code>wp_auth_check_load<\/code> filter turns that check off on every screen except the post editor, and Heartbeat is dequeued there too.<\/li>\n\n\n<li>The older version of this post deregistered <code>heartbeat<\/code> on <code>init<\/code> everywhere. WordPress still registers <code>wp-auth-check<\/code> and <code>autosave<\/code> with Heartbeat as a dependency, so every admin page logged a &#8220;called incorrectly&#8221; notice for missing dependencies, and post locks stopped working in the editor.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor, edit the config and publish. Leave <strong>Action\/Hook<\/strong> empty.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The settings are needed on the front end and in wp-admin, so <strong>Everywhere<\/strong> is the right trigger; <strong>Conditions<\/strong> would only cover the front end.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same code also works unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Only slow it down: set <code>'disable_front' =&gt; false<\/code> and keep <code>disable_admin<\/code> off. Then only the interval changes.<\/li>\n\n\n<li>Fewest requests: set <code>'interval' =&gt; 120<\/code> and <code>'disable_admin' =&gt; true<\/code>. The editor screens keep Heartbeat at two minutes.<\/li>\n\n\n<li>Override the config from another snippet with the <code>dpc_heartbeat_config<\/code> filter, for example to switch it off in wp-admin only for some roles.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>With <code>disable_admin<\/code> on, users whose login expires on a screen without Heartbeat find out only when they save. The editor screens still show the login popup.<\/li>\n\n\n<li>WooCommerce, page builders, membership and chat plugins sometimes use Heartbeat for live updates. If something stops refreshing, set the interval back to 60 first.<\/li>\n\n\n<li>The browser itself already slows Heartbeat down to 120 seconds in background tabs, so a lower interval mostly matters for the tab people are looking at.<\/li>\n\n\n<li>To see what still loads, open the Network tab and filter on <code>admin-ajax.php<\/code>, or <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/find-all-functions-hooked-to-a-wordpress-action-filter\/\">list everything hooked to<\/a> <code>heartbeat_received<\/code>.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Slow down the WordPress Heartbeat API and switch it off on the front end or in wp-admin, without breaking post locks or a plugin.<\/p>\n","protected":false},"author":1,"featured_media":46711,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Slow Down or Disable the WordPress Heartbeat API","description":"Slow down the WordPress Heartbeat API and switch it off on the front end or in wp-admin, without breaking post locks or a plugin."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[],"dpc_library":[204],"class_list":["post-10914","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=10914"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10914\/revisions"}],"predecessor-version":[{"id":46710,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10914\/revisions\/46710"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46711"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=10914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=10914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=10914"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=10914"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}