Skip to:
Content
Pages
Categories
Search
Top
Bottom

Is there an API for bbPress

Viewing 25 replies - 1 through 25 (of 76 total)

  • shantabhai
    Participant

    @shantabhai

    I’m also looking for the REST API for bbPress but unable to find info on it either.


    Robin W
    Moderator

    @robin-w

    I know nothing of rest, but there’s one for wordpress http://v2.wp-api.org/ does that help?


    Wilbur
    Participant

    @ozu

    I know nothing of rest, but there’s one for wordpress http://v2.wp-api.org/ does that help?

    No it doesn’t support bbpress


    Robin W
    Moderator

    @robin-w

    The below is offered as potential help, and in an effort to help, so if it is rubbish, then please come back, and I will try to help further.

    bbpress uses the standard wordpress posts, but with post types of ‘forum’, ‘topic’ and ‘reply’ instead of ‘post’ and ‘page’, and bbpress code itself basically uses wp_query to access the database.

    The glossary for wordpress api says

    GET /wp-json/wp/v2/posts to get a collection of Posts. This is roughly equivalent to using WP_Query.

    so I’d imagine that you would just use the same sort of call, and use wp_query parameters to return what you wanted.

    So if for wordpress you had a command that gave you say the latest 5 posts, then this same command would work but just changing whatever that code has for post_type=’post’ to post_type=’topic’

    If you wanted topics from a particular forum, you would add the equivalent of post_parent = ID where ID was the forum ID

    In essence if you can tell me what you want, I can give you the wp_query, and you could hopefully translate this into json.

    Does any of that make sense? πŸ™‚


    Wilbur
    Participant

    @ozu

    Sure it does make sense. With WP V2 API, if you want to list the ten latest posts, you use this url http://MY_SITE.COM/wp-json/wp/v2/posts. Please how do you do this to return that of forum topics?


    Robin W
    Moderator

    @robin-w


    Robin W
    Moderator

    @robin-w

    bit more digging

    it would appear that wordpress 4.7 takes out the filter capability

    https://github.com/WP-API/WP-API/issues/2964

    there is plugin that puts it back

    https://github.com/WP-API/rest-filter

    Otherwise I think they now want you to filter at the client end.

    so http://MY_SITE.COM/wp-json/wp/v2/posts gives you all the posts, and then you pick those that are the right post type.

    If you try the plugin above, then I think

    http://MY_SITE.COM/wp-json/wp/v2/posts?filter[type]=topic

    does it

    Please do report progress back !!


    Wilbur
    Participant

    @ozu

    Thanks. I tried http://MY_SITE.COM/wp-json/wp/v2/posts?filter[type]=topic and http://MY_SITE.COM/wp-json/wp/v2/posts?filter[type]=forum but the filter appeared not to work as blog posts was returned. Please not that I have already installed the plugin.


    Robin W
    Moderator

    @robin-w

    ok, I’ve found a link to some code that doesn’t entirely make sense yet, but gives me some clues, but I’ll do some more digging over the next few days.


    Wilbur
    Participant

    @ozu

    Okay. I am waiting. Thanks for the help do far.


    Barry
    Participant

    @barryhughes-1

    To enable default REST API support for topics you could use a snippet like this one:

    add_action( 'bbp_register_post_types', function() {
    	register_post_type( bbp_get_topic_post_type(), [ 'show_in_rest' => true ] );
    }, 11 );

    This waits until bbPress has registered its post types, then modifies the properties of the topic post type so that it is exposed via the REST API. You could of course extend it to cover forum and reply posts, too. With that in place, you should find URLs like the following work as expected:

    http://bbpress.site/wp-json/wp/v2/topic


    Barry
    Participant

    @barryhughes-1

    …A PHP 5.2-friendly version that also covers the reply and forum post types:

    function bbpress_enable_rest_api_support() {
    	$enable_rest = array( 'show_in_rest' => true );
    	register_post_type( bbp_get_reply_post_type(), $enable_rest );
    	register_post_type( bbp_get_topic_post_type(), $enable_rest );
    	register_post_type( bbp_get_forum_post_type(), $enable_rest );
    }
    
    add_action( 'bbp_register_post_types', 'bbpress_enable_rest_api_support', 11 );

    Robin W
    Moderator

    @robin-w

    @barryhughes-1 – huge thanks – the answer is always simple once you know !

    I’ve just tried

    http://mysite.com/wp-json/wp/v2/topic

    lists all topics

    http://mysite.com/wp-json/wp/v2/forum/16316

    lists forum 16316

    http://mysite.com/wp-json/wp/v2/topic/?sortBy=date

    lists all topics newest first


    Robin W
    Moderator

    @robin-w

    Just added it as a small plugin available on my site

    bbPress Rest API


    Robin W
    Moderator

    @robin-w

    I found that the above code took away the Forums, Topics and Replies from the dashboard in bbpress.

    I found some other code and modified it as follows

    /**
      * Add REST API support to an already registered post type.
      */
      add_action( 'init', 'rew_custom_post_type_rest_support', 25 );
      
      function rew_custom_post_type_rest_support() {
      	global $wp_post_types;
      
      	$post_type_name =  bbp_get_reply_post_type();
      	if( isset( $wp_post_types[ $post_type_name ] ) ) {
      		$wp_post_types[$post_type_name]->show_in_rest = true;
      		$wp_post_types[$post_type_name]->rest_base = $post_type_name;
      		$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
      	}
    	$post_type_name =  bbp_get_topic_post_type();
      	if( isset( $wp_post_types[ $post_type_name ] ) ) {
      		$wp_post_types[$post_type_name]->show_in_rest = true;
      		$wp_post_types[$post_type_name]->rest_base = $post_type_name;
      		$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
      	}
    	$post_type_name =  bbp_get_forum_post_type();
      	if( isset( $wp_post_types[ $post_type_name ] ) ) {
      		$wp_post_types[$post_type_name]->show_in_rest = true;
      		$wp_post_types[$post_type_name]->rest_base = $post_type_name;
      		$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
      	}
    	  
      }


    @barryhughes-1
    – thoughts – can this be done better?


    Wilbur
    Participant

    @ozu

    Hi Robin and Barry, thanks for the help so far, it is working now. Though I have noticed that the API is not as robust as I expected it to be. For example the /reply endpoint doesn’t show the details of of the person who posted the reply (author) and moreover, say I wanted to view all the replies for a particular topic; can this be done?


    Robin W
    Moderator

    @robin-w

    As far as I can see that is down to the rest api, not bbpress – it does the same with posts ie not show author.

    I suspect barry is way ahead of me in knowledge but these two links seem to show that you write api callbacks into your website and then add them to the api

    http://wordpress.stackexchange.com/questions/224332/how-to-get-author-meta-into-post-endpoint-in-api-v2

    http://v2.wp-api.org/extending/adding/


    Barry
    Participant

    @barryhughes-1

    Apologies all and good catch, Robin!

    I don’t think there’s anything wrong with your version of the code, but for the sake of alternatives here’s a fix for what I posted originally:

    function bbpress_enable_rest_api() {
    	$types = array(
    		bbp_get_reply_post_type(),
    		bbp_get_forum_post_type(),
    		bbp_get_topic_post_type(),
    	);
    
    	foreach ( $types as $slug ) {
    		$definition = (array) get_post_type_object( $slug );
    		$definition['show_in_rest'] = true;
    		$definition['rest_controller_class'] = 'WP_REST_Posts_Controller';
    		register_post_type( $slug, $definition );
    	}
    }
    
    add_action( 'bbp_register_post_types', 'bbpress_enable_rest_api', 11 );

    Beyond that, the links you shared (in terms of mixing in extra info such as the author’s details) look solid to me πŸ™‚


    Barry
    Participant

    @barryhughes-1

    My replies seem to keep disappearing into the bbPress void … trying again just to see if this one will show up.


    Barry
    Participant

    @barryhughes-1

    What do you know, it worked!

    So getting back to business – thanks for catching that error in my earlier snippet, Robin. I think yours is perfectly fine (also, great links re adding author meta) but just to ‘close the circle’ on my own snippet, here’s a revision:

    function bbpress_enable_rest_api() {
    	$types = array(
    		bbp_get_reply_post_type(),
    		bbp_get_forum_post_type(),
    		bbp_get_topic_post_type(),
    	);
    
    	foreach ( $types as $slug ) {
    		$definition = (array) get_post_type_object( $slug );
    		$definition['show_in_rest'] = true;
    		$definition['rest_controller_class'] = 'WP_REST_Posts_Controller';
    		register_post_type( $slug, $definition );
    	}
    }
    
    add_action( 'bbp_register_post_types', 'bbpress_enable_rest_api', 11 );

    Barry
    Participant

    @barryhughes-1

    Additionally, as an example of how you can add pretty much anything you want to the JSON responses returned by the REST API:

    function bbpress_rest_api_replies_extra_data( WP_REST_Response $response ) {
    	if ( ! is_array( $response->data ) ) {
    		return $response;
    	}
    
    	foreach ( $response->data as $key => $single_item ) {
    		// Only modify replies
    		if ( bbp_get_reply_post_type() !== $single_item['type'] ) {
    			continue;
    		}
    
    		// Add the author's details using a custom format
    		$author_id = get_post( $single_item['id'] )->post_author;
    		$author = get_user_by( 'ID', $author_id );
    		$response->data[ $key ]['author'] = "{$author->display_name} <{$author->user_email}>";
    	}
    
    	return $response;
    }
    
    add_filter( 'rest_request_after_callbacks', 'bbpress_rest_api_replies_extra_data' );

    You could say this is a bit of a brute force hack and although when combined with my last snippet it successfully adds the author’s display name and email address to replies (not topics), it shouldn’t really be used for that purpose “as is”: it doesn’t escape the author details, you probably don’t want to expose their email addresses, plus it’s out of alignment with how author meta is returned for other post types … However, I felt it was worth sharing an example of how you can quite quickly add arbitrary fields to REST API output.


    Robin W
    Moderator

    @robin-w

    @barryhughes-1 thanks for posting back, and for the examples.

    This is a whole new area, and whilst I am only interested out of eerrrmmm… interest, if others come up with things they need to achieve, I might be tempted to do a bit more.

    thanks again for your valuable contributions !!


    Barry
    Participant

    @barryhughes-1

    Absolutely – I’m also still exploring this new territory, nice to have an ‘excuse’ to do so πŸ˜‰

    Apologies @barryhughes-1, Akismet caught your previous posts as spam for some reason /shrug

    We’ve got an update that will at least let you know Akismet caught them as spam rather than just an empty void to deploy in the not too distant future πŸ™‚


    Barry
    Participant

    @barryhughes-1

    No worries!

Viewing 25 replies - 1 through 25 (of 76 total)
  • You must be logged in to reply to this topic.
Skip to toolbar