2

I have a plugin where I want to intercept database changes made during the upgrade process.

It looks like the WordPress Way to do this is with the 'dbdelta_queries' filter. The trouble is, my plugin's main .php file isn't loaded during the upgrade process, so I can't hook that filter.

How can I get a plugin to hook a filter -- this filter or any filter -- during the upgrade process?

1 Answer 1

2

Must-use (mu) plugins are loaded during the version update process, unlike ordinary plugins. Explanation here. So it's possible to hook dbdelta_queries and the related filters from within a mu plugin.

(Thanks to Sergey Biryukov for the answer.)

But, some plugins use the same dbDelta() function as core update, so it's possible to get queries for other tables to hook in the filter.

To avoid filtering anything except during core update, code like this will work.

add_filter( 'dbdelta_queries', 'my_query_filter', 10, 1 );
 
/** Filters the dbDelta SQL queries.
 *
 * @param string[] $queries An array of dbDelta SQL queries.
 * @since 3.3.0
 *
 */
function my_query_filter( $queries ) {
 
  // Look for the core update lock
  $lock_option = 'core_updater.lock';
  $lock_result = get_option( $lock_option );
  /* No lock found? We'e not doing a core update, so bail */
  if ( ! $lock_result ) {
    return $queries;
  }
 
  // Check to see if the lock is still valid. If it is, bail.
  if ( $lock_result > ( time() - ( 15 * MINUTE_IN_SECONDS ) ) ) {
    return $queries;
  }
 
  // Filter the queries as needed
 
  return $queries;
}

See the source code for WP_Upgrader::create_lock() to understand the 'core_updater.lock' option stuff.

And see here for a more complete writeup about how to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.