Store Just WordPress Transients Persistently

WordPress comes with an option of keeping cache in a persistent storage, if provided. It provides a great deal of performance since everything that WordPress was doing repeatedly on each page request now can be saved in the persistent cache storage (APC / Memcached / Redis) and retrieve easily & very fastly without doing much work.

The way to activate that is to drop an object-cache.php file in your wp-content directory which does the heavy lifting for you. Following plugins are available if you want to get started with it:

You don't have a reason to not use object caching unless you are stuck with a plugin which doesn't follow WordPress standards and causes the site to malfunction when a persistent cache storage is used. An example of such a plugin is Wishlist Member plugin.

It simply doesn't work well with persistent object cache because it updates the options table directly by making SQL queries instead of using functions like get_option(), update_option() etc.

I needed to solve the problem, where we could atleast store some of stuff in the persistent cache & hence improving performance. The site for which this was done, is a learning portal where all members are logged in, hence full pages were generated dynamically every single time. To take off some of the load, I developed a plugin in which transients are stored in memcached (persistent storage) so that they can be retrieved very quickly without making SQL queries to the database.

I suggest you use this plugin or use it as a starting point to build what you need. Plugins of this kind, require installation & verification by a developer since its a very barebones-prototype plugin, which does the job of saving transients and retrieving them quickly.

Plugin's repository -

It also overrides wp transient delete-all WP-CLI command so that you can flush transients from both database and memcached (persistent cache storage in use).

Its always a good idea to contribute back your work to the plugin if you build on top of it or extend it, so that other community members can gain from it.

Let me know if you have a question that I can help answer.

Override WP-CLI commands

If you already don't know about WP-CLI, its a pretty great tool to manage a WordPress install via terminal commands. This essentially gives you a lot of power enabling you to interact with the WordPress install just by commands and not having to interact with UI at all. Read more at WP-CLI's homepage.

It can also be extended to add custom commands. The usual way to do can be read in detail here - but the essence is to create a new PHP Class extending from WP_CLI_Command and then just add that class as a handler for a command by calling WP_CLI::add_command().

Now for instance, what if you want to redefine an existing wp-cli command? The way to do that is to create a new PHP Class as if you were creating a new custom command, and then instead of extending it from WP_CLI_Command, extend it from the PHP Class which defines the command you want to override. Simple enough! Here's an example:

wp transient delete-all lets you delete all transients from the database. Yes, database. If you know about persistent caching storage options with WordPress like Memcache or APC, then transients are not actually stored in database anymore. They are simply stored in the persistent cache storage. And since the delete-all subcommand of transient command only runs a SQL query to delete them from database, it simply does nothing if the site uses persistent cache storage (Object cache drop-in object-cache.php, if that reminds you of it).

So you can define a new PHP Class something like this:

if ( defined('WP_CLI') && WP_CLI ) {
class Transient_Mod_Command extends Transient_Command {
public function delete_all() {
// new definition of delete-all subcommand
WP_CLI::add_command( 'transient', 'Transient_Mod_Command' );

Pretty straight-forward, right?

Using the same technique, you can add new sub commands to an existing command as well by simply defining a new method.

On the transient issue note, why that shouldn't be fixed in WP-CLI itself is a question that intrigues me as well. From a quick look at their Github issues, seems like they want to support it in a way it works correctly with persistent cache storage, but haven't got to that place yet. I will see how soon I can create a patch for it and send a pull request.

Have fun with WP-CLI!

Filtering transient expiration time in WordPress

As of now WordPress, doesn't offer a filter to control the expiration of the transient while its being set, so I quickly wanted to share a quick snippet I found on Trac, written by Andrew Nacin.

Here is the snippet:

add_action( 'set_transient_my_transient', function( $value, $expiration ) {
$desired_expiration = 600;
if ( $expiration != $desired_expiration ) {
set_transient( 'my_transient', $value, $desired_expiration );

Basically, 'set_transient_' . $transient hook is fired once the transient gets saved, so it saves itself again resulting in recursion as the same hook will be fired again, but before it saves itself again, it checks whether the $desired_expiration is different from the $expiration with which it's being saved. Smart!

On a side note, if you ever need to do this, don't use anonymous functions because that takes away the ability to unhook your function by someone else. Its just a quick way to demonstrate the idea.

Concerned Trac Ticket -

How to use a single image on Facebook when your website is shared

Ideally, you would want to use context specific images to be shown on Facebook whenever any page of your WordPress website is shared on Facebook, but if for some reason you want only a single image to be shown irrespective of the context, that can be configured using my plugin - Facebook Like Thumbnail though it specifies context specific images by default.

You need to install the plugin, configure the default image to be used from the settings page. And then paste the following code in your functions.php file:

This short-circuits the logic of the plugin and specifies the default image specified in settings to be used for all pages.

I see this use-case as being very limited to what everyone would want, so I don't want this to be added as another option in the plugin itself, but it can be achieved using short-circuit opportunity offered by the plugin's architecture. Even if you want to plug in custom logic to figure out the image to use, that's possible too.

Let me know if you run into any issues.

WordPress plugin to email commentators of posts

The need to email came up when I wanted to notify commentators of my previous announcement posts regarding my plugin - Facebook Like Thumbnail, about its new version which is a rewrite and has lot of goodness in it. I looked it up, and I found a plugin by Yoast, which just adds all the commentators in Bcc and let you email using your mail client. I definitely wanted a better solution where they are emailed separately along with their actual comment so as to remind them that they left a comment on my site earlier and this email is in regard to the same topic.

I build a free plugin, which is now hosted on repo - Email Posts Commentators, and the way it works is, you can select multiple posts to be processed together. Specify your own email as Bcc and even lets you exclude email addresses from the comments of the selected posts. You write down your message along with email subject and an email goes out to them. Take a look at the screenshot to see how its admin screen looks like and how does the email itself look like.

Admin screen

Admin screen

Email that goes out

Email that goes out

If you run into issues, using the plugin, please report them here -

Updated Facebook Like Thumbnail WordPress plugin (v0.3.1) released

I just pushed out the latest version of my plugin Facebook Like Thumbnail, v0.3.1 to be precise, on plugins repo. It should show up in your WordPress admin shortly if you are using it and if not, then here is yet another reason to try the plugin out if random or unwanted images show up for your WordPress site when links are liked or shared on Facebook.

The new version is a complete rewrite and is really good at figuring out the context specific image to use for different types of pages. It also deals with situations where earlier version would run into compatibility issues with other plugins. And last but not least, this version is developer friendly in case you want to short-circuit the logic, you are able to do so now. All power to you.

Hit me up if you have any questions. And one real good of piece of advice that I can't mention too much is - Facebook maintains a cache of images selected for every URL (usually 24hours), so your new images won't show up right away, but eventually they will. Facebook has a really nice tool to debug your URLs outside of their cache to test the image being selected for display -

If you are looking for premium support - get in touch with me via my contact form.

Get WordPress images from a different site

Developers usually work on their local installs before pushing any changes to the live or production server and that's how a developer's workflow should be.

In order to setup a local or dev install on which a developer can work or test, they need to pull down the codebase and database dump to exactly mirror the site but they also need to download the media folder to make sure all images show up fine, which can have a bit of its own issues.

Codebase is usually under version control, so that's easy to pull and push changes. Database dump is a single file to download. But media folder can be very large sometimes and not possible to download every time you want to sync up with the files on live site. A super easy solution, that I use, is to setup a redirection rule in your local webserver to redirect all media requests to the live site and then you don't need to sync your media files at all.

Here is a single line, that can be added in your WordPress .htaccess file (if you are using apache, for nginx or any other webserver, idea stays the same, syntax will vary ofcourse):

RedirectMatch 302 ^/wp-content/uploads/(.*)$$1

Voila! Now you have all images showing up from live site directly 🙂

P.S. - This doesn't work if you are offline for obvious reasons.