Skip to content

Instantly share code, notes, and snippets.

@Alimir
Last active March 4, 2021 20:20
Show Gist options
  • Save Alimir/44ac0bb2117862d46b46e8af4acfffd2 to your computer and use it in GitHub Desktop.
Save Alimir/44ac0bb2117862d46b46e8af4acfffd2 to your computer and use it in GitHub Desktop.
How to add vote fields to WordPress comments API
<?php
add_action( 'rest_api_init', function () {
register_rest_field( 'comment', 'votes', array(
'get_callback' => function( $comment_arr ) {
return array(
'like_amount' => wp_ulike_get_comment_likes( $comment_arr['id'], 'like' ),
'dislike_amount' => wp_ulike_get_comment_likes( $comment_arr['id'], 'dislike' ),
'likers_list' => wp_ulike_get_likers_list_per_post( 'ulike_comments', 'comment_id', $comment_arr['id'], NULL )
);
}
) );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment