Using WordPress ‘heartbeat_autosave()’ PHP function

The heartbeat_autosave() WordPress PHP function performs autosave with the Heartbeat API.

Usage

heartbeat_autosave( $response, $data );

Example:
Input:

$response = array();
$data = array( 'post_id' => 1, 'content' => 'Updated content' );

Output:
Array ( [wp_autosave] => Array ( [success] => 1 ) )

Parameters

  • $response (array) – The Heartbeat response.
  • $data (array) – The $_POST data sent.

More information

See WordPress Developer Resources: heartbeat_autosave

Examples

Basic Usage

Performing autosave using the heartbeat_autosave() function with sample response and data arrays.

$response = array();
$data = array( 'post_id' => 1, 'content' => 'Updated content' );

$result = heartbeat_autosave( $response, $data );
print_r( $result );

Autosave a Custom Post Type

Using the heartbeat_autosave() function to autosave a custom post type named ‘book’.

$response = array();
$data = array( 'post_id' => 2, 'content' => 'Updated book content', 'post_type' => 'book' );

$result = heartbeat_autosave( $response, $data );
print_r( $result );

Autosave with Additional Metadata

Autosaving a post with additional metadata using the heartbeat_autosave() function.

$response = array();
$data = array(
    'post_id' => 3,
    'content' => 'Updated content',
    'meta' => array(
        'author' => 'John Doe',
        'publish_date' => '2023-05-06'
    )
);

$result = heartbeat_autosave( $response, $data );
print_r( $result );

Autosave with Custom Taxonomies

Using the heartbeat_autosave() function to autosave a post with custom taxonomies.

$response = array();
$data = array(
    'post_id' => 4,
    'content' => 'Updated content',
    'taxonomies' => array(
        'category' => array( 'WordPress', 'PHP' ),
        'post_tag' => array( 'autosave', 'heartbeat' )
    )
);

$result = heartbeat_autosave( $response, $data );
print_r( $result );

Autosave with Custom Fields

Autosaving a post with custom fields using the heartbeat_autosave() function.

$response = array();
$data = array(
    'post_id' => 5,
    'content' => 'Updated content',
    'custom_fields' => array(
        'location' => 'New York',
        'event_date' => '2023-05-20'
    )
);

$result = heartbeat_autosave( $response, $data );
print_r( $result );