-
Notifications
You must be signed in to change notification settings - Fork 185
WP REST API integration #68
Comments
I'll leave this open. Cheers. |
Any luck? I plan on revisiting this soon. |
I still want to take care of this. May take me a few days to get the free time, but I'll do my best to take care of it ASAP 👍 |
@robertdevore Are you still planning on working on this? I am looking into Gutenberg support and expect REST API support will be needed. |
@grappler I honestly haven't looked much into it, but in the next couple of weeks after I launch v2.0 of WP Dispensary, I'll have more time to put together some code for this 👍 |
I'll be adding this into the next release. Not sure about full-blown Gutenberg support but definitely REST API support, and then Gutenberg shortly after. The endpoint will probably just be at a location like |
There are two ways of adding support You can directly register the meta key which means it will get added to the meta section. add_action( 'init', function() {
register_meta(
'post',
self::SUBTITLE_META_KEY,
[
'type' => 'string',
'description' => '',
'single' => true,
'sanitize_callback' => [ &$this, 'sanitize_subtitle' ],
'show_in_rest' => true,
'auth_callback' => true,
]
);
} ); The second method is define a seperate section to display the subtitle. You can do that with add_action( 'rest_api_init', function() {
register_rest_field(
'post',
'subtitle',
array(
'get_callback' => function( $post ) {
return get_post_meta( $post['id'], '_subtitle', true );
},
'schema' => [
'type' => 'string',
'context' => [ 'view' ],
],
)
);
} ); Both snippets have been tested but I not sure if they are following the best practices. |
Quick-and-dirty kind-of-works solution. This can be better. I'll work on DocBlocks + better code organization before release: diff --git a/public/class-subtitles.php b/public/class-subtitles.php
index 80c35c7..5d00d84 100644
--- a/public/class-subtitles.php
+++ b/public/class-subtitles.php
@@ -202,6 +202,14 @@ if ( ! class_exists( 'Subtitles' ) ) {
*/
add_action( 'wp_head', array( &$this, 'subtitle_styling' ) );
+ /**
+ * Enable REST API Integration
+ *
+ * @see add_action()
+ * @since 4.0.0
+ */
+ add_action( 'rest_api_init', array( &$this, 'subtitle_register_rest_field' ) );
+
/**
* Filter post titles to display subtitles properly.
*
@@ -249,6 +257,28 @@ if ( ! class_exists( 'Subtitles' ) ) {
}
} // end method __construct
+ /**
+ * Reveal a post or supported post type's subtitle to the REST API.
+ *
+ * @see https://github.com/wecobble/Subtitles/issues/68
+ * @since 4.0.0
+ */
+ public function subtitle_register_rest_field() {
+ global $post;
+
+ register_rest_field(
+ 'post',
+ 'subtitle',
+ array(
+ 'get_callback' => function ( $post ) { return get_post_meta( $post['id'], '_subtitle', true ); },
+ 'schema' => array(
+ 'type' => 'string',
+ 'context' => array( 'view' ),
+ ),
+ )
+ );
+ }
+
/**
* Make sure that Subtitles plays nice with WordPress SEO plugin by Yoast.
* |
Issues:
|
Looks good. Though |
@grappler It should not be needed but it is. Without it, NULL is returned. In fact, I should only need to call |
Guess I should probably take care of this now. |
I've been doing some work with the WP REST API recently and noticed that the subtitles aren't in the data being shown for any of the content types (posts, pages, custom post types).
I've got some code that I'm currently using to add in the subtitles for each of the custom post types I'm working with, but I'm trying to find a way to elegantly write it so it loops through and automatically adds the subtitle to every content type. Doing this will allow it to work for any CPT that a website is using.
I figured I'd open this up at the very least to remind myself to get the code wrote and pushed for inclusion into the plugin. If anyone beats me to it, great. If not, I'm still working on it 👍
The text was updated successfully, but these errors were encountered: