Skip to main content
Tweeted twitter.com/StackWordPress/status/1067931275191902208
added 239 characters in body
Source Link
JakeParis
  • 663
  • 3
  • 7
  • 22

I would like to display a dropdown list of categories (or other taxonomy) inside of a Gutenberg block. I can only think of two ways to do this:

  1. Create an array of the taxonomy in php and use wp_localize_script to make that data available to my block javascript.
  2. Use fetch()? in the block to reach out to the api at /wp-json/wp/v2/categories/ and get all the categories.

Are one of these the preferred method? Is there some other kind of built-in or better way to do this?

Edit

As I continue to research this, I learned of another method available in the Gutenberg plugin and presumably available once this editor becomes part of core: wp.apiFetch(). Apparently, it's a wrapper around fetch which hides away some of the unnecessary parts. Now I'm wondering if this is the preferred method.

  1. Use wp.apiFetch() in the block to reach out to the REST api at /wp/v2/categories and get all the categories.

The Catch

On first glance, it seems to make more sense to use the apiFetch() function. However, because that's asynchronous, the data doesn't get loaded into the JSX element. I'm not sure how to make that data load into the element.

I would like to display a dropdown list of categories (or other taxonomy) inside of a Gutenberg block. I can only think of two ways to do this:

  1. Create an array of the taxonomy in php and use wp_localize_script to make that data available to my block javascript.
  2. Use fetch()? in the block to reach out to the api at /wp-json/wp/v2/categories/ and get all the categories.

Are one of these the preferred method? Is there some other kind of built-in or better way to do this?

Edit

As I continue to research this, I learned of another method available in the Gutenberg plugin and presumably available once this editor becomes part of core: wp.apiFetch(). Apparently, it's a wrapper around fetch which hides away some of the unnecessary parts. Now I'm wondering if this is the preferred method.

  1. Use wp.apiFetch() in the block to reach out to the REST api at /wp/v2/categories and get all the categories.

I would like to display a dropdown list of categories (or other taxonomy) inside of a Gutenberg block. I can only think of two ways to do this:

  1. Create an array of the taxonomy in php and use wp_localize_script to make that data available to my block javascript.
  2. Use fetch()? in the block to reach out to the api at /wp-json/wp/v2/categories/ and get all the categories.

Are one of these the preferred method? Is there some other kind of built-in or better way to do this?

Edit

As I continue to research this, I learned of another method available in the Gutenberg plugin and presumably available once this editor becomes part of core: wp.apiFetch(). Apparently, it's a wrapper around fetch which hides away some of the unnecessary parts. Now I'm wondering if this is the preferred method.

  1. Use wp.apiFetch() in the block to reach out to the REST api at /wp/v2/categories and get all the categories.

The Catch

On first glance, it seems to make more sense to use the apiFetch() function. However, because that's asynchronous, the data doesn't get loaded into the JSX element. I'm not sure how to make that data load into the element.

deleted 5 characters in body
Source Link
JakeParis
  • 663
  • 3
  • 7
  • 22

I would like to display a dropdown list of categories (or other taxonomy) inside of a Gutenberg block. I can only think of two ways to do this:

  1. Create an array of the taxonomy in php and use wp_localize_script to make that data available to my block javascript.
  2. Use ajax fetch()? in the block to reach out to the api at /wp-json/wp/v2/categories/ and get all the categories.

Are one of these the preferred method? Is there some other kind of built-in or better way to do this?

Edit

As I continue to research this, I learned of another method available in the Gutenberg plugin and presumably available once this editor becomes part of core: wp.apiFetch(). Apparently, it's a wrapper around fetch which hides away some of the unnecessary parts. Now I'm wondering if this is the preferred method.

  1. Use wp.apiFetch() in the block to reach out to the REST api at /wp/v2/categories and get all the categories.

I would like to display a dropdown list of categories (or other taxonomy) inside of a Gutenberg block. I can only think of two ways to do this:

  1. Create an array of the taxonomy in php and use wp_localize_script to make that data available to my block javascript.
  2. Use ajax fetch()? in the block to reach out to the api at /wp-json/wp/v2/categories/ and get all the categories.

Are one of these the preferred method? Is there some other kind of built-in or better way to do this?

I would like to display a dropdown list of categories (or other taxonomy) inside of a Gutenberg block. I can only think of two ways to do this:

  1. Create an array of the taxonomy in php and use wp_localize_script to make that data available to my block javascript.
  2. Use fetch()? in the block to reach out to the api at /wp-json/wp/v2/categories/ and get all the categories.

Are one of these the preferred method? Is there some other kind of built-in or better way to do this?

Edit

As I continue to research this, I learned of another method available in the Gutenberg plugin and presumably available once this editor becomes part of core: wp.apiFetch(). Apparently, it's a wrapper around fetch which hides away some of the unnecessary parts. Now I'm wondering if this is the preferred method.

  1. Use wp.apiFetch() in the block to reach out to the REST api at /wp/v2/categories and get all the categories.
Source Link
JakeParis
  • 663
  • 3
  • 7
  • 22

How would I get a taxonomy/category list inside a Gutenberg block?

I would like to display a dropdown list of categories (or other taxonomy) inside of a Gutenberg block. I can only think of two ways to do this:

  1. Create an array of the taxonomy in php and use wp_localize_script to make that data available to my block javascript.
  2. Use ajax fetch()? in the block to reach out to the api at /wp-json/wp/v2/categories/ and get all the categories.

Are one of these the preferred method? Is there some other kind of built-in or better way to do this?