• | |

    WordPress Database Encoding & the Case of Disappearing Widgets

    Recently I was updating the widgets on my homepage to promote the new WP in One Month. I decided to do so with the logo and a caption, making Jetpack’s Image Widget the perfect candidate. I uploaded the logo, added a title, then copy and pasted the text from the website’s ‘Jumbotron’ area for the caption. I clicked ‘Save’ and something strange happened: all of the Image Widgets, everywhere on my site, disappeared. I tried to do the same thing with a Text Widget, thinking maybe there’s a bug in Jetpack’s Image Widget or it doesn’t allow certain text/markup. After going through the same steps and clicking ‘Save’ every Text Widget on the site disappeared. I decided (really, was forced) to do some investigating.

    Read More “WordPress Database Encoding & the Case of Disappearing Widgets”

  • |

    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.

  • |

    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:

  • Quick Tip: Turn off Amazon Wishlist Spoilers

    While talking to my mother, whom I sent my Amazon Wishlist to for gift ideas, she told me to not check Amazon until Christmas because she bought stuff off of my list and didn’t want me to see what she got. Luckily, Amazon has thought of this, and there is a really simple way to make sure that you don’t see when people buy things off your list.

    1. Go to your Amazon Wish List.
    2. In the List Actions drop-down (towards the top of the list, on the right), click Update list profile.
    3. Check  “Don’t spoil my surprises.”
    4. Click Save.

    That’s it! You can now view your list without worry of spoilers.

  • GitGutter for Sublime Makes Diffs Easy

    At Crowd Favorite we use git for everything, which is a nice change from my old workflow of hoping for the best. I’ve learned quite a bit about git and Github since starting and have looked for tools to help me do things better. A really simple, but super helpful, tool is the GitGutter package for Sublime (thanks Dave!)

    It’s very straight forward: when you add, change, or delete a line, it will put small icons in the gutter of your file, with the line numbers, to make it easier for your to spot where you made modifications. It doesn’t seem like much, but I definitely miss it when I’m using a different editor or machine. Check it out!

    GitGutter for Sublime

  • Quick Tip: Check to See if a Slug Exists in WordPress

    Recently I was doing some work where I was importing RSS entries into a Custom Post Type in WordPress. Since there were no common IDs between the feed and WordPress, to prevent duplicate entries I tried comparing titles. This also proved to be an issue as titles aren’t always unique, and they weren’t in this case. I settled on comparing slugs; WordPress creates a unique slug for each post from the title, and there’s a way to know what that slug is going to be before the post is added.

    Read More “Quick Tip: Check to See if a Slug Exists in WordPress”