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:

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 -

Quickest way to get timestamp of a time in a particular timezone in PHP

Playing with timezone conversions can be crazy, it still confuses me every time and I have to wait a minute to run some test code to figure out what's it that I am trying to do here.

Anyway, the quickest way I have to get timestamp of a time in particular time zone is

DateTime and DateTimeZone classes are what you need to know when dealing with timezones, ditch any other method of doing conversions in PHP. Seriously, there are no better ways of doing it.

And just a tip, if you already don't know, timestamp from different timezones for a single point of time is same. It doesn't depend on the timezone. Did you know? :-)

Let me know in the comments if you have a question.

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.

Use WooCommerce product image as its category image

WooCommerce is capable of showing category as well as products on its shop page and this is configurable from settings available in admin side but I can totally imagine running into the situation that categories don't have their images uploaded and they will end up showing placeholder / default image. A quick solution would be to pull a product image from that category and use that image in place of thumbnail, highly dependent on what sort of products you are selling on your store, but in case you fit the bill, here's how to do so:

Install this as a plugin, you can download it. Activate the plugin and any product category will now pull image from a product under that category and show it. In case an image is uploaded for the product category or one already exists, that takes a higher priority and is used instead of trying to get one out of a product.