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