Last active
December 31, 2019 06:15
-
-
Save stnc/2fe47e1a4f966a0a866499c0c0702820 to your computer and use it in GitHub Desktop.
worpress Like WP REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/***************************************************************** | |
** Like WP REST API -start ******* | |
******************************************************/ | |
/** | |
* | |
* Registers REST API endpoints | |
* | |
* @since 1.0.0 | |
*/ | |
add_action('rest_api_init', 'get_Liked_Post_Meta_Data_InfoRegister'); | |
function get_Liked_Post_Meta_Data_InfoRegister() | |
{ | |
register_rest_route('wp/v2', '/stnc_like/id/(?P<id>[\d]+)', | |
array( | |
'methods' => 'GET', | |
'callback' => 'get_Liked_Post_Meta_Data_Info', | |
'permission_callback' => function ($request) { | |
return is_user_logged_in(); | |
}, | |
) | |
); | |
} | |
/** | |
* get post meta for _liked | |
* | |
* @since 1.2.0 | |
* @param WP_REST_Request $request The request sent from WP REST API. | |
* @return array Gets quiz list | |
*/ | |
function get_Liked_Post_Meta_Data_Info(WP_REST_Request $request) | |
{ | |
$postID = $request['id']; | |
$result = (get_post_meta($postID, '_liked', true)); | |
if ($result == '' or $result == 'NaN') { | |
return $result = 0; | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment