Access via REST API
-
Hello,
as far as I know, WordPress REST API will return only “modified” and “modified_gmt” (both of them are dates) for posts.
I’d like to have something like “modified_by” to return the user who modified a post. I believe this plugin adds this functionality as this is written in the description: Allows you to display last modified author info in posts, pages.
Is it possible to get the information about last modified author via REST API? And if so, how is it possible please?Thanks.
Dusan
-
Hi @dusang
May I know what you want to do by using last modified author name from REST API?
Thanks!
-
This reply was modified 7 years, 1 month ago by
Sayan Datta.
Hi Sayan,
We are using KnowAll theme (Knowledge base). I created a utility to generate a report of Knowledge Base articled modified in the last week, which contains Subject, Creation Date, Created By and Modified Date. The only information missing here is Modified By. I’m getting this information via WordPress REST API.
So that’s why I’m wondering if we install your plugin to get Modified By (the last person who modified Knowledge Base article), if I’ll be able to get this information via REST API.
Thanks,
Dusan
Hi @dusang
You just need to get the last modified author name of posts. Am I right? or any custom post types?
Hi Sayan,
Yes, you’re right. Even though it’s maybe a custom post type, because of that KnowAll theme we’re using. This is REST API URL for posts: https://COMPANY_URL/wp-json/wp/v2/posts/ and this is REST API URL for knowledge articles (what we use): https://COMPANY_URL/wp-json/wp/v2/ht-kb/
The schema for ht-kb is almost the same as schema for posts.
Thanks,
Dusan
Hi @dusang
Add this code to the end of your functions.php file:
add_action( 'rest_api_init', 'lmt_register_custom_api_field' ); function lmt_register_custom_api_field() { // Add the last modified author name to GET requests for individual posts register_rest_field( 'post', 'modified_by', array( 'get_callback' => 'lmt_cutomize_wp_rest_api_output', ) ); } function lmt_cutomize_wp_rest_api_output( $object, $field_name, $request ) { $modified_id = get_post_meta( $object['id'], '_edit_last', true ); if( $modified_id ) { $last_user = get_userdata( $modified_id ); return $last_user->display_name; } return ''; }And see the results. It will add a field
modified_byto rest data containing Last modified user name if exists.Thanks!
-
This reply was modified 7 years, 1 month ago by
Sayan Datta. Reason: Use of `register_rest_field()`
Hi Sayan,
Thank you once more for your quick answer.
If I understand you correctly, you will implement this in the next release. Is this correct?
I’m not using PHP to get all posts/knowledge bases. I wrote this utility in C#, so unfortunately your code snippet won’t help me. What I would need is an example REST API request how to query this modified_by field. Something like this: https://developer.wordpress.org/rest-api/reference/posts/ (they are querying all posts there).
So is there any REST API call where I can query
modify_by, please?Thanks
Hi @dusang
Have you tried this?
add_action( 'rest_api_init', 'lmt_register_custom_api_field' ); function lmt_register_custom_api_field() { // Add the last modified author name to GET requests for individual posts register_rest_field( 'post', 'modified_by', // you can use anything here like modify_by array( 'get_callback' => 'lmt_cutomize_wp_rest_api_output', ) ); } function lmt_cutomize_wp_rest_api_output( $object, $field_name, $request ) { $modified_id = get_post_meta( $object['id'], '_edit_last', true ); if( $modified_id ) { $last_user = get_userdata( $modified_id ); return $last_user->display_name; } return ''; }Hi Sayan,
No I haven’t as I’m not using PHP to query REST API.
But maybe I don’t understand you… I haven’t installed your plugin yet. So maybe this is a code to add to your plugin?
If this code is for your plugin, will I be able to querymodified_byby using REST API?Thanks.
Hi @dusang
WordPress REST API will return only “modified” and “modified_gmt” (both of them are dates) for posts.
As you told before, WordPress does not has any last modified author output in REST API. But this code snippet extends the REST API output as it adds the post last modified author name to the REST API output.
So, you can use that code to extend REST API output of WordPress then you can do anything like you want to query it in C#.
This feature is not added to the plugin yet. So, I give you that code to add it manually to your functions.php file.
For now this plugin just shows post modified author name to the frontend. In future release I may add this Rest Api feature to this plugin.
Thanks!
Hi Sayan,
Thanks for your answer.
I’m not admin of our WordPress, so I don’t see functions.php. I will give your code to our admin, so hopefully he will insert it into the right place.Thank you very much for your help.
Hi @dusang
Ok fine. Can you add plugin to your website? If you can Install Code Snippets plugin and create a snippet with my code and activate it. And then check REST API output.
Thanks!
Hi Sayan,
No I can’t. I will ask our admin to add that Code Snippets plugin.
Thanks.
Ok. Please let me know if it doesn’t works.
Thanks!
Hi Sayan,
Thank you very much for your help. I asked our admin to add your code to functions.php. Now REST API returns modified_by, but only for the endpoint wp-json/wp/v2/posts/. modified_by is empty however, but REST API returns only one record where date and modified are the same, so maybe it’s supposed to be empty.
Unfortunately for the endpoint wp-json/wp/v2/ht-kb/ there’s no modified_by value returned by REST API. I guess I have to contact the author of the KnowAll theme.
Once again, thank you very much for your help.
Thanks,
Dusan
-
This reply was modified 7 years, 1 month ago by
The topic ‘Access via REST API’ is closed to new replies.