|

Setting All Videos To Show Controls In LearnDash

One of the biggest pieces of feedback I’ve gotten on my course videos in recent weeks was that the controls on my videos were hidden. To be honest, I didn’t realize students couldn’t scrub through videos, or rewind – I assumed everything was working! After looking through the LearnDash settings and not finding a global option, I decided to the problem with just a little bit of code.

TL;DR: The Code

So I’ll cut to the quick and give you code first. Read on to learn about it! This is the setting you want: 

learndash_update_setting( get_the_ID(), 'lesson_video_show_controls', true ); 

The full code is in this public gist.

Updating the LearnDash Setting

A few months ago I dug through the LearnDash code for a similar plugin I wrote to convert ACF videos to native LearnDash videos. I figured there was a similar setting for video controls, and I was right. If you use the Chrome inspector, you can see the name of the control in the source.

So I grabbed the name of that form field and fed it into the function I discovered earlier:learndash_update_setting(), which accepts a post ID, the setting name, and the value.

Confirming it worked for a single setting, it’s time to write the loop that would update all settings.

The Loop

I won’t dig into the full mechanics of the loop, but I do want to point out the custom post types I needed to update: 

$lessons = new WP_Query(
    array(
        'post_type' => array( 'sfwd-lessons', 'sfwd-topic' ),
        'posts_per_page' => -1,
    )
);

I’m grabbing all posts that are lessons and topics within LearnDash. Then I loop through them all, updating the video controls settings.

This is slightly inelegant because I should really check to see if there is a video on the post before turning on controls, but it worked!

Hooking it in.

Now that we have a working function that will update all posts, we need to figure out where to hook it into WordPress. Honestly, there’s only one smart option for this, and it’s on plugin activation: 

register_activation_hook( __FILE__, 'jc_swap_videos' ); 

This will ensure the code only runs once, when the plugin is enabled.

If you are adding it to your functions.php file or something like that, you should do it at a point where it won’t disrupt end users, so I would choose an admin hook, like admin_init.

I would also make sure it executes once, then comment out the action so it doesn’t run every time you load the admin.

Future Proofing

Now, this only counts for existing posts and not future lessons and topics you might add. I’d recommend toggling the option as you add the rest, but if you really want to make sure you don’t forget, you could repurpose the code to set the option on post save. Something like this will work:

function jc_set_video_controls_on( $post_id ) {
      learndash_update_setting( $post_id, 'lesson_video_show_controls', true );
}
add_action( 'save_post', 'jc_set_video_controls_on' );

What Do You Think?

When you take online courses, do you prefer video controls on? I sure do! As a course creator, what reasons do you have for turning them off? Let me know in the comments!

Similar Posts

  • |

    New Design- Dynamics

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!This is part 3 of 5 of the mini series for my new design. In this part I will be talking…

  • |

    WordPress Helper Functions for Detecting IE

    The other day I was working on a problem where I wanted to check if a website was using a specific browser (in this case IE) and version (in this case 9 or below). I came up with 2 functions that would serve an a nice, reusable check for both. These can also be extended…

  • Pivoting WP in One Month

    Do you know the story of Pixar? It’s a really interesting one that I strongly recommend you read all about in Creativity Inc. Here’s the gist: Pixar started off as a piece of hardware inside LucasFilms, and the team was tasked with pushing the technological envelope for live-action movies. When Lucas had to sell off…

  • The way to learn WordPress is not by contributing

    has happened in WordPress this year, and what we can expect moving forward. As always, he extolled the virtues of open source and the importance of contributing. He spent several minutes on Five for the Future, a program encouraging companies who make money with WordPress to give 5% of their time to the open source project. And while I strongly recommend contributing, I don’t think it’s the path someone should take when they first learn.

  • |

    How to Improve Performance on Your WordPress Site

    Did you know that 80% of website visitors will abandon your website if it takes more than 5 seconds to load? That’s a lot of lost revenue and opportunities. WordPress gets a bad wrap for for being slow, but it’s not usually WordPress’ fault! If you use the right tools, you can have a nice,…

  • My Travel Schedule for Fall 2019

    Every year there seems to be a season where I travel more than usual, and this year it starts today! Here’s a list of conferences (or cities) I’ll be visiting now through the end of the year. If you’re going to be around at the same time, let me know!

One Comment

Comments are closed.