Use the First Image in a Post as the Thumbnail in WordPress

To add a thumbnail to a post in WordPress, you need to manually add a “featured image” from the post editor. It’s possible to automatically use the first image in a post as the post’s thumbnail. Many themes do this automatically. There are also plugins that do this; one such plugin is Auto Post Thumbnail. However, if you don’t want to download a whole plugin, you can simply use the snippet below…

Enable a WordPress plugin for just one page

There are some plugins that are only used for specific pages on your blog. If you enable them globally, they will slow down the rest of your blog. WordPress doesn’t come with a built-in feature to conditionally enable plugins. I can’t even find a WordPress plugin that does this.

If you want to enable a WordPress plugin for just a single page, add this snippet to your functions.php:

For example, I’m using The SEO Framework as my SEO plugin…

Using APCu with PHP 7, WordPress, and W3 Total Cache

APC consists of 2 parts: opcode caching and memory caching. PHP 5.5 and newer have built-in opcode caching, so APC is no longer supported. A new PECL package, APCu, was created for the memory caching part. The prefix for APC memory caching functions changed from “apc” to “apcu”. For example, apc_store became apcu_store and apc_fetch became apcu_fetch.

APCu Backwards Compatibility

Some plugins, such as W3 Total Cache, are still using the apc_* functions…

W3 Total Cache Optimization: Caching Post Taxonomies

Today I installed Query Monitor and noticed that each post on my homepage was triggering 2 database queries. One query retrieved the post_tag post taxonomy and one retrieved the post_format post taxonomy. Since my homepage has up to 15 posts, there can be 30 extra queries. These values should have been fetched all at once; they should not be fetched from the database individually.

Database queries logged by Query Monitor

I disabled my plugins one-by-one and realized that the issue was caused by W3 Total Cache

Make all links have relative URLs in WordPress posts

WordPress’s TinyMCE editor makes all links use absolute URLs with your blog’s siteurl as the base. Your blog’s siteurl is the value in Settings > General Settings > WordPress Address (URL). This value is hardcoded in your posts. This means that if you change your domain, change your permalink structure, or switch from http to https, you’ll have to update your posts. Also, you cannot support both http and https; you have to choose one…

Load jQuery from Google’s CDN in WordPress

WordPress loads jQuery from your server by default. It’s much better to load jQuery from Google’s CDN (content delivery network). Here are the 4 main reasons:

  1. Your visitors probably already have jQuery from Google’s CDN cached. This means jQuery will load nearly instantly.
  2. It’s probably faster to load jQuery from Google’s servers than your servers. They have faster servers and better networks. They also probably have a data center that’s closer to your visitor.