wordpress

Using Thesis Post Image with WordPress Featured Image

Recently, I was tasked with converting a client’s site from a Thesis 1.8.5 theme to a new custom theme. For the most part it was pretty straight-forward, however one sticky issue I came across was recycling the post images originally used by Thesis for use with the new theme. I had a few options:

#1 Track down each image and manually insert them into 50+ posts.

Nope. Not happening. I have Netflix to watch.

#2 Pay for a plugin that’s supposed to do the trick.

If I do this, how am I supposed to pay for Netflix?

#3 Figure out a solution myself.

There we go.

The Solution

The Thesis Post Image was a feature provided by Thesis before the WordPress Featured Images (also known as Post Thumbnails) were introduced. Each image was saved along with the post’s Metadata. Grabbing the thesis image’s file path from the metadata we can use that to insert it into the featured image.

Using the post_thumbnail_html filter, a method I learned from Justin Tadlock’s post, we can modify the HTML that gets returned by the the_post_thumbnail() function. Add the following to your functions.php file.

add_filter( 'post_thumbnail_html', 'thesis_to_featured' );
function thesis_to_featured( $html ) {
    global $post;

    // Get the thesis post image
    $thesis_img = get_post_meta( $post->ID, 'thesis_post_image' );

    // Is there a featured image?
    if ( empty( $html ) ) {
        // Do we have a thesis image to work with?
        if( !empty( $thesis_img ) ) {
            // Overwrite the $html variable with the thesis image
            $html = '<img src="' . $thesis_img[0] . '" />';
        }        
    }        

    return $html;
}

Basically the script works like this:

  1. Get the thesis image, if there is one, from the post’s metadata.
  2. Check to see if the post has a featured image, if so do nothing and return the unmodified HTML.
  3. If there isn’t a featured image, check to see if the post has a thesis image.
  4. If it does, modify the returned image to use the thesis file path.

A Note

  • This script DOES NOT update the post’s featured image to the thesis image. This only displays the thesis image (if there is one) when a featured image is not found.
wordpress_and_jquery

Resolving “$ is not a function” error in WordPress

While working on my first WordPress plugin (which you will be hearing about soon), I came across the following error in my console.

$ is not a function

“Alright, jQuery isn’t being included. That’s weird,” I initially thought.

WordPress includes jQuery by default, but maybe I’d made some stupid mistake that has thrown off the entire WordPress universe — it wouldn’t have been the first time.

I viewed the page source.

“It’s there, other scripts are working. Why has JavaScript forsaken me?” I was confused.

Maybe my script was being included in the wrong place?

wp_enqueue_script( 'script', plugins_url( 'js/script.js', __FILE__ ), array( 'jquery' ) );

Nope. I was using wp_enqueue_script to register the script and included jQuery as a dependency. Everything was as it should be.

There was, however, one thing I missed:

The jQuery library included with WordPress is set to the noConflict() mode (see wp-includes/js/jquery/jquery.js). This is to prevent compatibility problems with other JavaScript libraries that WordPress can link.

In the noConflict() mode, the global $ shortcut for jQuery is not available…

Ah.

Luckily there was a quick, easy fix. Originally the script opened like so:

$(document).ready(function() { ..

Which is tried and true. However, as we’ve learned, that’s not going to work like we’d prefer. So, as the codex suggests, all we need to do is make a quick update:

jQuery(document).ready(function($) {

That’s all you need to do. Replace “$” with “jQuery”, then pass “$” to allow for its use within the function.

Very nice.

Matt Learns Python: Part 3 – String Formatters… or how I learned to stop worrying and love the modulo

The goal of this blog is to document my journey learning new things in the world of web development and programming. At the same time, I hope to help others learn as well. Feel free to say hello on Twitter!

Previous Post: Matt Learns Python: Part 2 – More Strings, Variables, and Math!

Hello everyone and welcome back to my series on learning things, specifically Python. Let’s jump right in. Open up your command line and start python. Type in the following:

>>> name = "Matt"
>>> print 'My name is %s' %name
My name is Matt

See that little %s? That’s called a formatter, or an interpolation operator. The % is called a modulo (which would make for a great band name). I like to think of it as a placeholder, where the formatter is replaced with the variable specified at the end.

Let’s try this:

>>> name = "Matt"
>>> age = 87
>>> print 'My name is %s and I am %d.' % (name, age)
My name is Matt and I am 87.

We’ve added another formatter (%d) and variable (age). Now do this:

>>> print 'My name is %s and I am  %d.' % (age, name)

Whoops:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str

So what happened? Well, there are a number of different conversion types. The two we’ve used in this example are for strings (%s) and signed integer decimals, also known as numbers (%d). The formatters and variables must be used in the same order. When we switched age and name, our formatter was now looking at name for a number. name is a string, not a number so we were served with a TypeError.

This has been just a quick introduction, we’ll delve a bit deeper in the next post.

Further Reading: