LearnDash REST API v2-Quiz Questions… musings

If you’ve ever worked with LearnDash then you know they have a robust REST API developed. Quite an extensive set of functionality. About 4 years ago (from today’s post date) they started releasing V2 of the API. For the past 4 years they’ve made statements that it’s still in BETA.

Well, their definition of BETA release is not my definition of BETA. To me, a beta release is one where all code functionality is there. It may be broken, or have bugs, but it’s there, and the documentation is near 98% defined and up to date. Unfortunately LearnDash has a ways to go. Their documentation for V2 is spotty (in specific instances of mentioning OBJECTS as schema variables) they don’t define what’s IN the objects… and in some cases functionality is just plain missing. In other cases the functionality is seriously incomplete.

I’ve been working with a specific REST API functionality for V2-Questions. That would be Quiz Questions. The callback is missing CREATE QUESTION (while the doc alludes to the functionality being there) – the callback validates, but there is no code behind it. When I looked at the defined class file, sure enough – NO CODE.

So what happens when you call the function today? It creates a custom post type called a sfwd-question, but it doesn’t link it or add the appropriate metadata to the WP PRO QUIZ tables. In other words, basically useless.

So to solve this problem, I originally modified the core file they ship with the product. Which worked – I embedded the correct code to execute after wordpress post insertion for rest api.. Life is good right? well, yes – that is until LearnDash issues an update to their application and wipes out all my custom code.

Then I pondered how to do this. How do I maintain my code line, while leveraging all that LearnDash has to offer? Including their base-classes and inheritance? My answer? (which worked by the way) is as follows:

  1. Make a copy of the root class file
  2. Rename the class file and the CLASS itself
  3. Move the copy to my own plugin
  4. Instantiate my plugin
  5. Voila – my code should be available right?

WRONG!!!

Turns out: I needed a few more things to make this work.

  1. Set the REST-INIT to execute AFTER LearnDash REST-INIT (ensure that all the LearnDash classes. This provided my inherited REST-API class file to load and inherit the LearnDash base REST post class.
  2. Change my end-point, so that it’s not in conflict with the LearnDash end-point.

This corrected my code, and leveraged the base classes that LearnDash provides. When I call my specific end-point from PostMan, everything works and responds exactly as expected. This gives me the option to keep my own code along side what LearnDash offers, at the same time – basically “ignore” the LearnDash endpoint for quiz questions UNTIL they fix it, and it works properly.

This approach may or may not work for all of your efforts, but it’s certainly worth a try. In the end, I hope that LearnDash will fix the core class file. Once they do, then I can delete my class, and return to calling their REST end-point.

IF you do this, remember to NEVER extend what the core offers (because sooner or later you will want to use the core file for maintenance reasons).
Hope this helps, Dan

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *