LearnDash: Course Enrollment Using Ajax

We are using LearnDash along with MemberMouse at work and I thought I’d write a short article showing you how to enroll a User/Customer into a course via Ajax. We are going to do two things at once, first subscribe the User/Customer to the Course and then redirect them to the first lesson in the course. Note that I’m assuming you are order the Lessons in your courses by the Menu Order and second that you are just using Course -> Lessons without Topics.

Enroll Button

You are going to have a lot of freedom with your Enroll button but we are going to keep it very simple.

Register WordPress Ajax Action

Let’s move on to setting up the Ajax call. We are going to allow the customer to Enroll if they are logged in, so we are not going to call wp_ajax_nopriv. The following code goes in your functions.php file or in your plugin if you are adding this custom functionality via a plugin.

We are going to be using the LearnDash function ld_update_course_access(), this function takes 3 parameters, the User ID, Course ID and a boolean that when set to TRUE, it removes the User/Customer from the course instead of enrolling him/her in the course.

The Javascript Component: Setting Up The Ajax Call With jQuery

The following code makes the call to the server and sends the required Course ID parameter so we can Enroll the User/Customer in the course.

That’s It

Questions? I would love to hear from you if you have any questions about this article or if you have any questions related to LearnDash in general. I would be more than happy to help you figure it out.

Everything APIs Related

I’ve been contemplating for a while now the possibility of beginning to write a weekly series on working with different modern APIs within WordPress’ environment or using a recommended or pre-existing PHP class.

I’ve already have the first two┬áI would really like to explore much deeper Eventbrite and JW Platforms API so these two would be the first ones I write about.

Do you have any requests or recommendation for useful APIs every developer should know about and feel comfortable using? Please let me know in the comments section of this post.

Has It Been That Long?

Has it been a little over 5 months since I started working at KelbyOne as a Web Developer? It’s incredible how fast time goes when you are having fun and working in such a relax environment. The atmosphere is great, co-workers are a pleasure to talk to and everybody is always in a good mood.

Professionally I’ve grown exponentially. Went in knowing very little about OOP and right now all the code I write is OOP. Had to learned to use different, like REST APIs like EventBrite and JW Platform to name a few.

I honestly can’t really complain about anything, very happy of where I’m right now and where I hope to be and call my home for many years to come.