• Hi, I am using this plugin for my community which is linked with another application. Can you please help me out with any REST API available for this plugin

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter shashwatsrivastava48

    (@shashwatsrivastava48)

    Hi, sorry for not explaining the scenario, actually what I need is to call a REST API to like/dislike a topic/forum from a Java application. Using REST API we are planning to create a topic and like it from java application. So for creating topic we do have BBpress rest API. Similarly do we have any REST API for like/dislike topic/replies etc ?

    Hi @shashwatsrivastava48

    I am working on similar thing. Here is my progress.
    First to show no.-of-likes detail with other detail of posts (detail which we get via “https://domain.com/wp-json/wp/v2/posts/”) I used following codes which I found here.

    add_action( 'rest_api_init', 'slug_register_spaceship' );
    function slug_register_spaceship() {
        register_rest_field( 'post',
            '_liked',
            array(
                'get_callback'    => 'slug_get_spaceship',
                'update_callback' => 'slug_update_spaceship',
                'schema'          => null,
            )
        );
    }
    /**
     * Handler for getting custom field data.
     */
    function slug_get_spaceship( $object, $field_name, $request ) {
        return get_post_meta( $object[ 'id' ], $field_name );
    }
    
    /**
     * Handler for updating custom field data.
     */
    function slug_update_spaceship( $value, $object, $field_name ) {
        if ( ! $value || ! is_string( $value ) ) {
            return;
        }
    
        return update_post_meta( $object->ID, $field_name, strip_tags( $value ) );
    
    }

    In above code “_liked” is the meta_value of plugin which use to store in wp_postmeta and with above code you will see likes count with this name “_liked” via /wp-json/wp/v2/posts/.

    By this code I can see post-likes details also in /wp-json/wp/v2/posts/.

    This is how I am sending likes info to app.

    Now to get and update like info from app here is my plan.
    First when I will get any likes(in digits with post id) from app then I will pick that number and there will be auto update query will run that will change like-count of that post in wordpress-Database.

    In wordpress database there is table named as “wp-postmeta”. This table we need to update likes. To update table I will get post-id from app and run query like this.. If post-id = this and meta_key = ‘_liked’ then update meta_value = this (something like this).
    In postmeta table the field “meta_key” stores the meta value of this plugin(which is “_liked”) and “meta_value” field stores likes-count.

    So this is how I am doing it right now. If you have found any better solution then please share. Else see if this helps you. Bur if you mange to do it in different way then don’t forget to share. I might pick up your way if it is better and easier.

    • This reply was modified 7 years, 3 months ago by rish30990.
    • This reply was modified 7 years, 3 months ago by rish30990.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Is there any REST API available for the plugin ?’ is closed to new replies.