Category Archive: Essentials

Debugging AJAX

I can debug my JavaScript using console.log(), which also displays code errors. I can debug my PHP code using print_r() (which I prefer because it’s leaner; many use var_dump()). Since I have PHP’s display_errors directive enabled, and usually I declare error_reporting( E_ALL ^ E_STRICT ) or error_reporting( -1 ), so errors are output within the browser.*

So I can immediately read ... 

Read article

Image Function Cheat Sheet

Simply a reference of the six image-related functions I use most often. I have this list printed out and keep it handy; for your convenience I’ve linked to the WP doc page for each.

In case you weren’t aware, many of these accept either a named size (e.g. ‘small’,'medium’,'full’,'your_custom_name’) or an array of [width,height] (e.g. array(300,200)).

wp_get_attachment_image

Returns HTML tag, something ... 

Read article

Preload Images

Here’s an easy way to manage your image preloads from anywhere in your PHP functions. We can simply build a globalized array of all image sources prior to outputting the page, then hook into the wp_footer action to output the required Javascript.

Each time your code encounters an image source that must be preloaded, simply call wpcx_add_preload_img($src) to add it to ... 

Read article

Conditional Excerpt Function

I use this function on practically every site that has any posts/archives section. This enables us to display a standardized excerpt inside or outside the Loop.

Arguments (all optional):
$ex_length the word count
$postref the post or text to excerpt. Can be an ID, a post object, a block of text, or if called within the Loop, null or not given ... 

Read article

Disabling autosave

You may already know, custom post types are the real workhorses of most customized WordPress themes. But often, they’re not meant to be read by the public; they’re functional things like ‘albums’ or ‘reservations’ or ‘index cards.’ By default, WordPress tries to create auto-drafts of any post type when an author is adding / editing through the admin panel, and ... 

Read article

Command-line helpers

Here are two helpful command-line (Linux) tools which are immensely helpful when working on large themes, particularly if you’re modifying someone else’s work. Both require you to first navigate to the directory in which your files reside (see below for a primer).

1. Find all instances of something.

This will respond with all files in which your “something” resides (quotes not needed ... 

Read article

No comments!

WordPress is now commonly used as a content-management system for many websites, without any need for common blog functions. Or, the blog functions are announcement-only, comments are unwelcome.

Even if you turn off comments for all posts, the admin backend can still get polluted by robot comment requests. That’s why it’s a good idea to disable them completely when they’re not ... 

Read article