Thumbnails 1.0.7 With a Couple of New Options

Thumbnails is a light plugin which helps your theme to generate thumbnails. Most of the thumbnails can be generated on the fly and stored in a cache folder, instead to full fill your upload folder. More, you change theme and the thumbs sizes changes you can be in trouble with old images.

With Thumbnails there is no more problems.

The new feature are the persistent autowired featured image which really improve the performances of the older versions and the ability to process images for the core sizes (limited to the thumbnail size of WordPress).

Header and Footer With AMP Support

Was time for a review of Header and Footer, the plugin many of you use to inject codes in the head, footer and middle of the posts and pages. The most common injections are:

  • Google Analytics, specially with customizations
  • Facebook Pixel
  • AdSense
  • Verification metatags

The new version removes the Open Graph minimal support since that protocol is now complex and needs a dedicated plugin. The bbPress support will be removed in the near future, so the plugin is lighter for who does not use bbPress and for who uses bbPress I released a more interesting and complete Ads for bbPress plugin.

AdSense for bbPress

Injecting ads on bbPress pages is now really easy with Ads for bbPress.

This new free plugin can inject, like Header and Footer whatever code you need before and after the bbPress pages not not only. It can inject between topic and replies and before the new topic and new reply form.

A must have for everyone running bbPress.

And it’s ultra light: the best practices have been used to load the blog as less as possible.

Find it on WordPress here.

Use get_option() Only Once, As Late As Possible and Store It

I see many plugins to get their options (one or more) with the super useful get_option() of WordPress not only once but every time they needed it and sometime more than once inside the same function.

This is definitively not good for performances. Let me explain why.

Admitting your options are “autoloaded”, your get_option() does not trigger a query to the database but every time you call it:

  • the option name is trimmed
  • the filter “pre_option_nnn” is called
  • the list of missing option is taken from the cache and checked
  • the list of autoloaded options is checked (probably creating a big array copy)
  • the option name is checked against ‘siteurl’, ‘home’, ‘category_base’, ‘tag_base’ special names
  • the maybe_unserialized is called over your option value
  • the filter “option_nnn” is applied

Reference: the options.php file of WordPress.

Every step is taken every time you call get_option(). It’s time to optimize that!

How to optimize the get_option() call?

First, use it as late as possible. Why? Suppose you created a plugin which just runs a shortcode. If that shortcode depends on a set of options but it is rarely used, why to run code which is not required?

To optimize the options load you can code it in this why (my example uses functions to be easy, but you should always wrap the code in a class). I assume you save all the options in an array (WordPress takes care to serialize and unserialize it).

As you can not the options are not returned by “my_init_options()” to avoid an array copy and because they anyway are global. Using a class and an internal property for your options makes the code less “dirty” and less “old style”. Optimized code most of the times is not elegant.

Let start the discussion, now!

Clean Up WordPress User Meta Data Table

During a maintenance of a blog, I needed to clean up the user metadata table since many users was deleted from the main user table (wp_users) with a direct query.

The quick and dirty solution is to run a “left join delete”. Here the SQL statement:

Easy and really quick (please avoid to clean up with a delete plus a sub-query which is the slowest method in the world – other than do it by hand row by row).

Akismet Optimization

I started a war again the resource wasting on Periodo Fertile. That blog count an important number of pageviews and even if the server has a lot of power it was suffering.

But that sound strange to me since with the same visitor load, months ago the server was not so overloaded. Why? WHY?

A number of plugins have been installed/update and the theme changed. Since I know how PHP coder work, I was suspecting under optimized code was raising the problem.

It is not Akismet, but while digging into the code, I found a little optimization for Akismet but the concept has general value.

Do not get_option() if the option it doesn’t exist

WordPress at startup loads all the options marked as “autoload”. It caches them and every call to get_option() is matched to the cached option names before try to read from the database.

But is an option is not present in the options table, it cannot be cache and the first get_option() call generates a query.

In Akismet you can find:

but the option “akismet_comment_nonce” is missing since there is not a place where to set it. Hence that generate a new query into the database on EVERY page load.

If an option cannot be set form the administration panel we can assume it is false, so I temporary change the code to look like:

and asked for support to know if my assumption is correct. Maybe it is not, always ask the authors!

Conclusion

Always set your options as “autoload” if you need them on every page load, you’re saving a database query for each option not already loaded. Be sure all you options are present in the options table, specially when you add new options and the user does not save them from the administration panel. Update the new options into the database with default values.

In my case, another updated plugin added 10 new options and no one saved them on its administration panel. So every page load in the blog was running 10 useless queries, over the 100 query needed to generate the whole page.

 

MD5 in Java Compatible With PHP md5() Function

The request was to have a Java function generating the same MD5 string generated by PHP when using the md5() function. Here the code snippet:

Please, note the check on the “toString()” result which must be padded with a zero if needed.

Have a nice hashing!

MD5 in SQLServer 2005+ Compatible With PHP md5() Function

The request was: replace the clear password on a big account database with its MD5 version as a string like the one we get with the md5() PHP function. There are a lot of suggestions on Stack Overflow, but many of them work only with SQLServer 2008 or greater version. Of course before to understand that limit I tried all the tips on my database (ok, not knowing it was a 2005 version).

But in the end there is a simple and definitive and tested (by me) solution to this stupid problem. Here the query to execute to replace a field with its own MD5 has like the one returned by PHP.

Of course you should replace the “users” table with your own, and the “password” fields with your own. Pay attention the password field is repeated in two place inside the query.

Hope this can be of help! For who work on SQLServer 2008 or greater there are simpler version of this query using the “convert” T-SQL function.

Donations From April 2015 To June 2016

Long time since my last donation of donations, but it has been a complicated period. What matters is to keep on tracks, even if a little bit late! The total amount of donations is 300 euros (about 340 dollars), donated to Save The Children.

As you know I think the right for a “normal” childhood is what can change tomorrow World.

Thank you, Stefano.