Where is the proper place to add a filter for pre_get_table_charset?

Seems like your assessment is correct and that specific function will be called before you will have a chance to add the filter in a traditional way. The related code flow is a call relatively early at boot time to wp_not_installed to check if wordpress is installed which calls in turn is_blog_active which tries to read the siteurl option, and therefor a DB query is made and the validation of table structure is done.

There seems to be two ways around it

  1. Have an object cache. You should in any case, but in this specific case the option will most likely be retrieved from the cache and no DB access will have to be done, at least not that early.

  2. Write a drop-in. Drop-ins are loaded much earlier in boot, and you probably have two options here, to override the DB class with the db.php drop-in or add an advanced-cache drop-in and set your filter there.

While it sound horrible to change the DB driver, in practice it is relatively safe as the drive do not change much between versions. Obviously the easier path is the advanced cache if you don’t have object cache.

Obviously you should probably first investigate why is the query slow. from the description it doesn’t seem like something that can get slow.

Leave a Comment