Code

  • Deleting 3 Million Spam Comments from a WordPress Database

    Over at the Crowd Favorite blog, I wrote up a post about an interesting problem I solved recently. I laid out everything you need to know there, but it involves downloading a HUGE database and putting WP-CLI to good use. If you haven’t used it before and you do development with WordPress, it’s super valuable. Anyway, check out the post – it’s a good one!

    Deleting 3 Million Spam Comments from Your WordPress Database

  • 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 IE Conditionals to wp_enqueue_style

    When creating a WordPress theme, it’s best practice to use wp_enqueue_style for adding all stylesheets, including style.css. At first glance, this can pose a challenge if you want to conditionally include CSS based on the browser (like IE-only styles, for example). Luckily, there is a quick way to do this in WordPress using $wp-styles:

    global $wp_styles;
    wp_enqueue_style( 'jlc_ie_styles',get_template_directory_uri() . 'css/ie-style.css', array(), '1.0.0' );
    $wp_styles->add_data( 'jlc_ie_styles', 'conditional', 'IE' );
    

    The code above calls on the $wp-styles class to associate our IE-only stylesheet (by tag/name/slug) with a condition, the condition being “IE.” If you wanted IE 9 and below, you could do this:

    $wp_styles->add_data( 'jlc_ie_styles', 'conditional', 'lte IE 9' );

    This is a great (and best practice) way to conditionally call styles. You can see more examples with comments over in this gist by wpscholar.

  • Fix wp_nav_menu on Custom Type Archives in WordPress

    Recently I was working with an issue in WordPress where the site’s menu was not showing up on an archive page for one of my custom post types. There were a few troubleshooting things I tried, including the most common recommendation, use theme_location instead of menu when referencing the menu in your theme (code after the jump):

    (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.