Code

  • New Plug-in: Latest Post Redirect

    One thing I often forget to do is update social links when I’m promoting a new blog post or podcast episode. Especially on Instagram, I’ll say something like, “link in the profile,”1 only to forget to add the link to the profile! Services like Linktree help a little, but you still need to update. Well over a couple of days, I built a plugin to help me (and hopefully you) with this little problem.

    (more…)
  • How I Built It is Officially Hosted by Castos!

    Castos is a fantastic WordPress-centric podcast hosting platform that I’ve been recommending in my courses and coaching calls. But I have a confession to make: since Castos launched after I launched my podcast, I’ve been using Libsyn.

    For a long time, I’ve considered making the switch, but the migration process always seemed so daunting. On top of that, I’d need to make updates to my website and it never felt like the right time. Well that changes today!

    (more…)
  • 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!

  • Fleshing Out My Work Week

    I have a confession: I’m in a bit of a funk. April was not a very productive for me. We were in Disney World for 8 days, and the rest of it, I was either sick or recovering from being sick. Trying to balance the different types of work I do and properly boxing time has also become a problem. Couple that with some distractions at home, and I haven’t felt very productive lately. I want to try going to a coworking space once a week, but I’d have to schedule it on a day where I don’t need my recording setup. So I’ve decided to set my work week with days dedicated to different types of work.

    (more…)

  • How Do We Best Teach Programming to Beginners?

    I’ve been thinking a lot lately about how I teach. I tend to take a “learn by doing” approach in my online courses where there are very clear, step-by-step instructions completed via video. However, this format gets pretty tough to execute in other contexts. For example, I teach an online graduate course for the University of Scranton, which is primarily text-based. This course’s goal is to get students with a healthcare background proficient in programming; the assumption is they are at least somewhat technical. After getting feedback, especially this semester, I’m realizing the approach my co-author and I took in creating the course was wrong. This got me thinking: how do we best teach programming to people who have never seen it?

    (more…)

  • 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 to check for other browsers or versions, or even accept custom regular expressions.

    (more…)

  • Add Attachments to WordPress Search Results

    I feel like this has to have been done a lot, and there are great plugins out there for it, but if you’re just looking to add a quick function to your theme (or a really simple plugin) yourself, here’s how to modify WordPress’ search query to include attachments (like images).

    function attachment_search( $query ) {
        if ( $query->is_search ) {
           $query->set( 'post_type', array( 'post', 'attachment' ) );
           $query->set( 'post_status', array( 'publish', 'inherit' ) );
        }
     
       return $query;
    }
    
    add_filter( 'pre_get_posts', 'attachment_search' );
    

    This does 2 things: includes posts of type ‘attachment’ to the search query, and adds the post status of ‘inherit’. This will ensure that any images (or other attachments) that were added while adding a new post or page will be included in the results.

    You can also extend the post type array to include your own Custom Post Types (eg array(  'post', 'attachment', 'products' ); )

    These 2 posts were very helpful in getting this code together:

  • Adding the Media Uploader in WordPress

    …without including the Editor. In the Admin. That was a really long title, so I hope you don’t feel mislead! I was recently working on a project that required a Custom Post Type without the editor, but needed the Media Uploader.

    Note: This is not a full-blown tutorial. The purpose of this post is to help those troubleshoot the fact that the media uploader is not working, given the conditions above. 

    Here is how I define the media custom meta box (this is only part of a bigger array of arguments):

    array(
        'title' => __( 'Upload File', 'jlc' ),
        'type' => array( 'custom-post-type' ),
        'id' => 'upload-file',
        'items' => array(
            array(
                'type' => 'media',
                'name' => '_upload_file',
                'label' => __( 'Upload File', 'jlc' ),
                'label_position' => 'before',
            ),
        ),
    ),
    

    In the post type definition, here’s what the ‘supports’ argument looks like:

    'supports' => array(
        'title',
        'page-attributes',
        )
    

    Note that the editor is not listed. If it were, the media uploader scripts would be automatically added. Instead, you get an error that wp.media is undefined. Luckily, there’s an easy fix for this. Simply add this line in where you custom post type is defined:

    add_action( 'admin_init', 'wp_enqueue_media' );

    This says that when you are on the admin, add the required media scripts. That way, even if the editor isn’t loaded, the media uploader will be.