Skip to content

Instantly share code, notes, and snippets.

@stnc
Last active September 19, 2019 23:53
Show Gist options
  • Save stnc/176277cebc6b822f176f59cab91cfd01 to your computer and use it in GitHub Desktop.
Save stnc/176277cebc6b822f176f59cab91cfd01 to your computer and use it in GitHub Desktop.
wordpress ulike rest api add-on -- https://wordpress.org/plugins/wp-ulike/
<?php
//ulike add post
add_action('rest_api_init', 'stnc_register_ulike');
function stnc_register_ulike() {
register_rest_field('post', '_liked', array('get_callback' => 'stnc_get_ulike', 'update_callback' => 'stnc_update_ulike', 'schema' => null,));
}
/**
* Handler for getting custom field data.
*/
function stnc_get_ulike($object, $field_name, $request) {
return get_post_meta($object['id'], $field_name);
}
/**
* Handler for updating custom field data.
*/
function stnc_update_ulike($value, $object, $field_name) {
if (!$value || !is_string($value)) {
return;
}
return update_post_meta($object->ID, $field_name, strip_tags($value));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment