POST contra

With this endpoint of the WordPress REST API, you insert a negative vote of a post. Below you will find an example of how to use the API. It is important that you create a unique string for the user that identifies the user.

Endpoint

POST domain.com/wp-json/helpful/contra/

@param string $user_id
@param int $post_id

Example

Here you can find an example of use:

/* Request URL */
$url = 'domain.com/wp-json/helpful/contra/';

/**
 * Here we now enter the user who should publish the post.
 * As password we use the generated password by applications passwords from admin user profile.
 */
$user = '<benutzername>';
$pass = '<applications passwords>';

/* Request header */
$headers = [
    'Authorization' => 'Basic ' . base64_encode($user . ':' . $pass),
    'cache-control' => 'no-cache',
];

/* Request body */
$body = [
    /* Unique user identification: */
    'user_id' => Helpful\Core\Helpers::get_user(),
    /* Post ID: */
    'post_id' => 1234,
];

/* Alternative request endpoint */
$url = 'domain.com/wp-json/helpful/contra/?user_id=12345&post_id=12345';

/* Request arguments */
$args = [
    'headers' => $headers,
    'body' => $body,
    'method' => 'POST',
];

/* Our request */
$response = wp_remote_post($url, $args);

/* If the request returns an error, the error is output */
if (is_wp_error($response)) {
    print_r($response->get_error_message());
}

$response = wp_remote_retrieve_body($response);
$response = wp_json_decode($response);
$response = intval($response);

if (1 === $response) {
    echo 'Vote was saved';
}

if (0 === $response) {
    echo 'Vote was not saved';
}
Was this post helpful?
Top