Title: customCSS in JSON API missing
Last modified: August 11, 2022

---

# customCSS in JSON API missing

 *  Resolved [Chris Coyier](https://wordpress.org/support/users/chriscoyier/)
 * (@chriscoyier)
 * [3 years, 9 months ago](https://wordpress.org/support/topic/customcss-in-json-api-missing/)
 * At one point, this plugin successfully output `customCSS` in the JSON API as 
   part of the post it was pulling, but now it comes back as `null`.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Hardeep Asrani](https://wordpress.org/support/users/hardeepasrani/)
 * (@hardeepasrani)
 * [3 years, 9 months ago](https://wordpress.org/support/topic/customcss-in-json-api-missing/#post-15909789)
 * Hey [@chriscoyier](https://wordpress.org/support/users/chriscoyier/),
 * As far as I remember, we had discussed it, and it was added with the help of 
   this code snippet: [https://wordpress.org/support/topic/custom-css-in-the-rest-api/#post-12968470](https://wordpress.org/support/topic/custom-css-in-the-rest-api/#post-12968470)
 * Can you confirm that the snippet still exists on your website?
 * Let me know, and I’ll take a deeper look to make sure it still works!
 * PS: Also, do you have the Otter Blocks plugin installed on your website?
    -  This reply was modified 3 years, 9 months ago by [Hardeep Asrani](https://wordpress.org/support/users/hardeepasrani/).
 *  Thread Starter [Chris Coyier](https://wordpress.org/support/users/chriscoyier/)
 * (@chriscoyier)
 * [3 years, 9 months ago](https://wordpress.org/support/topic/customcss-in-json-api-missing/#post-15912012)
 * Ah yes — that custom code, exactly as written, is still live in the functions.
   php of the active theme.
 * Here’s an example of JSON output of a page that has custom CSS on it, where the
   customCSS is null:
 * [https://blog.codepen.io/wp-json/wp/v2/landing_page/13826](https://blog.codepen.io/wp-json/wp/v2/landing_page/13826)
 * > do you have the Otter Blocks plugin
 * Nope.
 *  Plugin Author [Hardeep Asrani](https://wordpress.org/support/users/hardeepasrani/)
 * (@hardeepasrani)
 * [3 years, 9 months ago](https://wordpress.org/support/topic/customcss-in-json-api-missing/#post-15913236)
 * Hey [@chriscoyier](https://wordpress.org/support/users/chriscoyier/),
 * This should do the work:
 *     ```
       add_action( 'rest_api_init', 'add_post_css_rest_field' );
   
       function add_post_css_rest_field() {
           register_rest_field( 'post', 'customCSS', array(
                  'get_callback'    => 'get_post_css',
                  'schema'          => null,
               )
           );
       }
   
       function get_post_css( $object ) {
           //get the id of the post object array
           $post_id = $object['id'];
           if ( class_exists( '\ThemeIsle\GutenbergBlocks\Blocks_CSS' ) ) {
               $content = get_the_content( $post_id );
               $content = parse_blocks( $content );
               if ( ! is_array( $content ) || empty( $content ) ) {
       	    return null;
               }
               $class = new \ThemeIsle\GutenbergBlocks\Blocks_CSS();
               $css = $class->cycle_through_blocks( $content, 0 ); // 0 here is Post ID, since you aren't using Otter, 0 should be fine, or any post ID for that matter.
               if ( ! empty ( $css ) ) {
                   return $css;
               }
           }
   
           return null;
       }
       ```
   
 * Sorry, a few months back, we had a major reshuffle for our block plugins and 
   had to change the class names. This should do the work and we don’t think we 
   will be changing the class name ever again.
 * Let me know if it helps!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘customCSS in JSON API missing’ is closed to new replies.

 * ![](https://ps.w.org/blocks-css/assets/icon-256x256.jpg?rev=2030330)
 * [Blocks CSS: CSS Editor for Gutenberg Blocks](https://wordpress.org/plugins/blocks-css/)
 * [Support Threads](https://wordpress.org/support/plugin/blocks-css/)
 * [Active Topics](https://wordpress.org/support/plugin/blocks-css/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/blocks-css/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/blocks-css/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Hardeep Asrani](https://wordpress.org/support/users/hardeepasrani/)
 * Last activity: [3 years, 9 months ago](https://wordpress.org/support/topic/customcss-in-json-api-missing/#post-15913236)
 * Status: resolved