Wordpress Heartbeat API cannot parse data
I want to use WP-API heartbeat to open a connection to two displays and have them reflect what the other is saying with each send
and tick
.
It used to work when the heartbeat API came out in 3.6 - but now at the latest version it is outputting an error:
SyntaxError: JSON Parse error: Unexpected identifier "SyntaxError"
I have tried following other answers on the web to parse the json error or find where it is failing but nothing seems to come out out of it other than it is returning the same url than the response from the server.
function mb_heartbeat() {
wp_enqueue_script( 'heartbeat' );
add_action( 'wp_footer', 'mb_heartbeat_footer' );
}
//our js to send/process
function mb_heartbeat_footer() { ?
script
$(document).ready( function() {
// send the user id
$(document).on( "heartbeat-send.mb-location-change", function( event, data ) {
data.mb_user_id = $('input[name="mb_user_id"]').val();
});
// receive the location
$(document).on( "heartbeat-tick.mb-location-change", function( event, data ) {
data.mb_user_location $("input#mb_user_location" + data.location ).prop( "checked","checked" );
});
// log errors
$(document).on( "heartbeat-error.mb-location-change", function( event, jqXHR, textStatus, error ) {
console.log( textStatus + ' ----------- ' + error );
})
});
/script
?php }
// server-side code
function mb_heartbeat_received( $response, $data, $screen_id ) {
$mb_userid = ( empty($data['mb_user_id']) || !is_numeric($data['mb_user_id']) ? null : $data['mb_user_id'] );
$mb_userid = absint( $mb_userid );
if( !$mb_userid ) {
return $response;
}
$response['mb_user_location'] = get_user_meta( $mb_userid, 'mb_user_location_current', true );
return $response;
}
// do it
add_action( 'init', 'mb_heartbeat' );
add_filter( 'heartbeat_received', 'mb_heartbeat_received', 10, 2 );
add_filter( 'heartbeat_settings', 'mb_heartbeat_settings' );