0

Im fairly new to WP development and am trying to create a plugin that generates a widget depending on the settings set in the custom post type. The widget is written in vue.js, and i would like to supply it with data from the WPDB (that i download and refresh using the plugin).

So a user can create a new custom post, set variables (example: URL of where the data is) and other settings inside that post, and then use the shortcode to display the widget.

I've created the plugin, read about custom post types and the REST API and created the frontend Widget.

My problem is this:

  • How do i get Data from the DB to the frontend (vue.js)? Can i use the REST API for the WPDB?
  • How can i use a custom post type to "feed" information into my widget? I assume i can just get the data from the WPDB about my custom post (storing the settings as part of the custom post), but that just brings me back to the first question.
  • Do i use a "post template" to "inject" my widget, or is there something else for that?
0
  • How do i get Data from the DB to the frontend (vue.js)? Can i use the REST API for the WPDB?

The REST API works for this, or you could include the data in the HTML as JSON or a window variable

  • How can i use a custom post type to "feed" information into my widget? I assume i can just get the data from the WPDB about my custom post (storing the settings as part of the custom post), but that just brings me back to the first question.

The REST API, this is pretty much the same question as above. Note that fetching posts in WP via WPDB is highly unusual, and is commonly considered a code smell and bad practice. WP_Query is a far superior/easier method

  • Do i use a "post template" to "inject" my widget, or is there something else for that?

You could do that, but it would be easier to build a block or shortcode

Keep in mind, that retrieving the data and putting/embedding something in a post are on topic, but Vue.js is offtopic, you'll need to consult Vue.js communities on how it's used.

  • hey tom, thanks for your reply! can i call the wp_query in javascript? I thought i have to use the REST API for that because the wp code is not exposed. I read about the localize_script function. Can i use that to expose the wp_query? – fogx Jul 4 '19 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.