Code

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.

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?

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.

A Commitment to Committing

If I can say one thing about working at Crowd Favorite, it’s that I’ve learned a lot about git and Github. From using the basic command line tools to working closely with submodules, I can say I understand git much more today than I did 3 months ago. Earlier this week, my friend Colin challenged me to 30 weekdays of committing to projects on Github. Starting today, I will take him up on that.

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.

WordPress and .gitignore

Updates have been added after the jump!

In the coming weeks I’ll likely put a lot of my own personal code (as in, not code people pay me to write) in public repos on Github. A lot of my work is WordPress related so I’ll make a local repo at the root of some WordPress install as to not complicate things. In order to make sure I don’t put up something unintentionally (like say, my wp-config.php file), I’ve created a more thorough .gitignore than what I have seen from automatic generators. If you’re interested, I put it into a Github Gist, so you can just copy and paste it. If you think I’m missing something or am wrongfully excluding something, leave a comment on the gist!

Github Gist: wordpress-ignore