1

As far as I know I haven't used Ajax anywhere in my code. Yet, while debugging, I have breakpoints being hit with 'admin-ajax.php' at the root of the call stack. This second hit of my breakpoint occurs between 10 and 30 seconds after the original. The weird thing is, as of yet there is no HTML output, so no scripts are present in the browser.

Any ideas why or where this is happening and why my php scripts are being called twice?

2

This is probably the new Heartbeat API. It is running in intervals from 15 to 60 seconds and offers a simple way to communicate with WordPress per AJAX in the background.

You can disable it in JavaScript with wp.heartbeat.stop(); and in PHP with remove_action( 'admin_init', 'wp_auth_check_load' ); (might not be enough on some pages).

  • It was the heartbeat API. Confirmed by the POST values. I won't disable it, but now that I know what it is I can add an early condition. – Twifty Sep 1 '13 at 7:28
  • @Waldermort See also debugging AJAX. This makes it easier to see what is going on. – fuxia Sep 1 '13 at 7:31
  • I've never had the need to use Ajax, though this project will later require it. Yet another language I'm going to have to learn... – Twifty Sep 1 '13 at 7:39
  • wp.heartbeat.stop(); doesn't seem to be in code anymore. :( – Rarst Feb 12 '14 at 10:37
2

Plenty of related snippets out there seems to no longer apply because code was considerably refactored.

This seems to be closest I got to stopping it without exploding things (as of 3.8):

add_action(
    'admin_print_footer_scripts',
    function () {
        ?>
        <script type="text/javascript">
            jQuery(document).ready(function ($) {
                $(window)
                    .off('blur.wp-heartbeat-focus')
                    .off('focus.wp-heartbeat-focus')
                    .trigger('unload.wp-heartbeat');
            });
        </script><?php
    }
);
  • Hmm. I still cannot decrease the load time for admin-ajax. – Chirag Feb 17 '14 at 13:07
  • @Chirag hm? this has nothing to do with load time, this is to stop Heartbeat pings from happening (which I just needed for dev, not a good idea for production) – Rarst Feb 17 '14 at 13:16
1

Placing this in functions.php turns off Hearteat API

add_action( 'init', 'my_deregister_heartbeat', 1 );
function my_deregister_heartbeat() {
    global $pagenow;

    if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow )
        wp_deregister_script('heartbeat');
}

I've not tested it myself but you can check out more here: http://wordpress.org/support/topic/admin-ajaxphp-being-called-from-admin-pages-causing-db-connection-issues

  • For some reason this breaks seemingly unrelated things. Notably folding of meta boxes, but likely more. – Rarst Feb 16 '14 at 14:11
0

To disable the heartbeat check you can use this code:

add_action( 'init', 'my_deregister_heartbeat', 1 );
function my_deregister_heartbeat() {
    global $pagenow;

    if ( 'post.php' != $pagenow && 'post-new.php' != $pagenow )
        wp_deregister_script('heartbeat');
}

source: @azaozz/core.trac

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.