• Resolved tbaikamine

    (@tbaikamine)


    following the exact steps in the heartbeat official documentation doesn’t simply work.
    and that’s super frusturating.
    here is what I did in case someone is trying to reproduce the problem and see it yourself:
    simply created 3 files in wp-content/plugins
    test.php

    <?php
    // Plugin Name: test
    add_action("admin_menu","f_admin_menu");
    function f_admin_menu(){
        add_menu_page("title_123","test_menu_123","manage_options","admin_menu_123",'f_menu_page');
    }
    function f_menu_page(){
        wp_register_script("jQuery","https://code.jquery.com/jquery-3.5.1.min.js");
        wp_register_script('all_script',plugin_dir_url(__FILE__) . 'all_script.js');
        wp_enqueue_script('jQuery');
        wp_enqueue_script('all_script');
        include_once('admin_menu_123.php');
    }
    function myplugin_receive_heartbeat( $response, $data ) {
        $response['myplugin_customfield_hashed'] = sha1( $data['myplugin_customfield'] );
        return $response;
    }
    add_filter( 'heartbeat_received', 'myplugin_receive_heartbeat', 10, 2 );
    ?>

    all_script.js

    $(document).ready(function(){
        $(document).on( 'heartbeat-send', function ( event, data ) {
            console.log('pulse sent')
            data.myplugin_customfield = 'some_data';
        });
        $(document).on( 'heartbeat-tick', function ( event, data ) {
            alert( 'The hash is ' + data.myplugin_customfield_hashed );
        });
    })

    admin_menu_123.php

    <?php ?>
    <input type="button" value="click">

    my wordpress details:
    latest version 5.5.1 hosted in my local wamp in a win10 64bits if that matters (even though am sure it doesn’t but someone will ask saying it\s the problem)
    what I expect:
    what is already obvious, a log in my console saying that i at least simply sent an AJAX heartbeat to serverside.
    what happens: NOTHING.
    again: this code is copied from the OFFICIAL documentation page

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • That page does say

    The Heartbeat API is a simple server polling API built in to WordPress, allowing near-real-time frontend updates.

    The key word is frontend.

    Your code doesn’t seem to have a proper plugin header (unless you left that out), and it is for an admin page.
    You are also using an external version of jQuery, which is not supported.

    Thread Starter tbaikamine

    (@tbaikamine)

    You are right about the second part of your answer, i did overwrite the jQuery version (also tried to re-queu the heartbeat script and it didn’t work)
    When i work with the one wordpress provides, the heartbeat works also.
    It sucks to see wordpress uses version 1.12 while jQuery is now 3.5
    Really sucks big time ! Especially with a super slow plan to “safely” upgrade in 2 years

    However the frontend “keyword” is wrong. Because the heartbeat worked in the admin side
    So the keyword, be it with the importance it was giving, is sadly misleading and so wrong. So the documentation must change, in case “frontend” has that much significance.

    • This reply was modified 3 years, 6 months ago by tbaikamine.

    OK, I can drop a note to the docs team to look at that paragraph.

    Also, jQuery will be updated in WP 5.6 which is coming soon.(beta is next week)
    There were a LOT of plugins with problems when WP 5.5 removed the old jquery-migration script. It’s a big effort.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘heartbeat simply doesn’t work’ is closed to new replies.