|

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

  • | |

    Directory Handler at Theme Forest

    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!I’m happy to announce my first Theme Forest component, Directory Handler. it’s a simple set of 2 classes that handle (in…

  • |

    WP Engine Easter Egg Saves You 20%

    I got a hot tip in my inbox today from one of my favorite hosting companies, WP Engine. For a limited time, when you visit their website and put in the Konami Code (below), you’ll save 20% on your purchase. To activate this discount, enter the following code using arrows and letters on their keyboard when…

  • Quick Tip: How to Move a WordPress Site

    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!I won’t go too in-depth here, but I will describe the steps you need to take to move a WordPress website…

  • |

    How to Learn a New Programming Language

    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! As I said in the last two posts, Google I/O was truly inspiring. It got me to thinking about how…

  • |

    Out Now: Responsive Design with WordPress

    I’m happy to announce that my second book, Responsive Design with WordPress is out today online and in bookstores. It’s published through Peachpit/New Riders and I couldn’t be happier with the way it turned out.  

  • |

    Join Me for CigarCamp at WordCamp US!

    With WordCamp US quickly approaching, I thought it would be fun to have an informal CigarCamp US while everyone is in town. Here are the details: Location: Ashton Cigar Bar (map) Date: Friday, December 4th Time: 9:00pm – 11:00pm EST Head on over to the site and sign up! There will be cigars, scotch, and WordPress talk. And…

One Comment

Comments are closed.