Category Archive: Snippets

Developer bookmarks

Here are a couple of quick-access bookmarks that perform searches directly on WordPress’ Developer Resources. I like to have these handy for quick lookups; they simply prompt me for an input string and go straight to WP search results. (You may need to enable JavaScript within your browser’s URL bar.)

First, a good one to use when you can’t quite remember ... 

Read article

Improving Nonce Security

WordPress nonces are pretty good at ensuring your ajax calls and form submissions are legit. A nonce is a coded string generated by incorporating the (logged-in) user ID, the $action string, and a timestamp-based “tick” value. The “tick” changes (globally) every 48 hours by default.

The nonce key is unique for each logged-in user. However, what if your site ... 

Read article

Image Upload Adjustments

When uploading images, WordPress’ default behavior is to skip any defined image size if the uploaded image is too small to fill it. If the image’s width or height (but not both) is big enough, WP shrinks to fit within the rectangle but does not fill it. When both the width and height are big enough, WP always center-crops to ... 

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

Admin-triggered e-mail

Here’s something that touches on lots of topics. A ‘send e-mail’ button within an admin panel, that opens up a ThickBox window with a standalone form and wp_editor.

The metabox

This requires two functions: one hooked into ‘add_meta_boxes’ which calls the second, the metabox output function:

Most of the second function builds the URL for the button’s href attribute. It points to a ... 

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

Draft status highlighting

A client wanted to display items with ‘Draft’ status differently (gray color) in the admin list pages. Some might argue I’m a sloppy coder for using inline CSS, but there’s something I like about targeted post types, particularly in admin pages. Anyway, here’s a good example of using the ‘admin_footer’ action and certain global variables, very useful.

This snippet (stick it ... 

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