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 counts 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!


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.

Comment Plus 1.4.3

This new version fixes the issue when an administrator replies to a comment directly from the administration console and the notification is not sent. Now should work correctly with the latest WP version.

Header and Footer: Inject In The Middle Of The Post

This new fabulous feature of Header and Footer has been requested few times and I introduced it for a my personal need on a big blog I manage.

How does it work?

Rather simple: a generic block of code (in my case a snippet of JavaScript) had to be included just before the first <h2> subtitle in posts but only if the <h2> subtitle was “enough” far away from the post main title.

It is not easy to control all that in a scientific was, but we are not scientists, we need only to put an ad in the middle of a post.

So the natural rule were: add this piece of cool code just before the first <h2> you find but start searching for it after 700 characters from the start of the post. You know a post contains HTML tag so 700 characters and actually not 700 human readable characters, but still we are no scientists.

This rule is so clear that it toke to me few minutes to implement (of course using the code already written in that big blog in a custom plugin…).

Another interesting way to find an injection point is the <p> tag, or the closing </p> tag. For example you may want to add something just after the first post paragraph. The rule will be: add that just after the first </p>, of course starting to search from the top of the article.

Why to inject before or after an HTML tag? Rather simple, because you don’t want to break up your so carefully written article, right?

There are special case on may want to manage: what if the <h2>tag is not found on a specific article? Maybe we want to recycle that code and inject it at least on top or bottom of the article (you know, loose ad impressions is not good). The rule has an option for that as well.

Now it’s up to you: add your own rules and inject the ads in the most profitable position and… share with me your results!

Header and Footer 2.0.0

This is a really important Header and Footer release since you will find a new interface, CodeMirror with syntax highlight, better mobile codes management. Hopefully without too many bugs even if it is a major release.

The most important new feature is the injection in the middle of the post, or better an engine that injects following you own rules. More on this topic will follow with a couple of articles.

Injection on bbPress forums, posts and replies should be more stable and easy to configure. There are even generic injections which search for some code in the generated page and replace them. More on that on next articles.