Which plugin calls that option?

Would you like to know which plugin is calling a ceratin option that you see in the database?

Have already googled the option name, but no results?

If you are struggling to understand where come that damned option from,  this blog post is for you.

How to check which files are calling a certain database option visiting a specific page.

The method that I’m going to show you will tell you which files call the option that you are checking when you visit a specific page.

It will not tell you the situation of the entire website, but only what happens when you load a specific page.

Let’s do an example. We want to know which files call the option “elementor_version” when we visit the homepage.

It looks like something very difficult, but the solution is quite simple.

First of all, enable the debugging in wp-config.php by adding the following lines of code before the comment /* That’s all, stop editing! Happy publishing. */:

 
define('WP_DEBUG', true);
define( 'WP_DEBUG_DISPLAY', false );
define( 'WP_DEBUG_LOG', true );

 

Then add the following code to the functions.php of your child theme if any, or to a functional plugin:

jose_log_files_called_by_option( 'elementor_version' );
//It writes which file calls the option
function jose_log_files_called_by_option( $option ){
    add_action( 'pre_option_'.$option,function( $value,$option ){
       $uri = sanitize_text_field( $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] );
       $backtrace = debug_backtrace();
       $file = 'no file';
       if( isset( $backtrace[3]['file'] ) ){
           $file = str_replace( ABSPATH,'',$backtrace[3]['file'] );
           error_log( sprintf( 'Page: %s; option: %s; file: %s',$uri,$option,$file ) );
       }
       return $value;
    },20,2 );
}

 

Then visit the homepage, or whatever page you want to check.

The function jose_log_files_called_by_option will add an action hook to ‘pre_option_{$option}’, where $option is the name of the $option that we want to check. In our example, the option name is ‘elementor_version’.  We have seen that option in our database, and we want to know which files call it.

Inside the closure, we use the function debug_backtrace which will give us an array with all the information we need.

The fourth element will be an array  that will look like

Array(
    [file] => /path-to-the-main-directory/wp-content/plugins/elementor/core/upgrade/manager.php
    [line] => 915
    [function] => get_option
    [args] => Array
    (
        [0] => elementor_version
        [1] => 1
    )

)

 

We need nothing else than logging the path given by [file].

After visiting a page you will find in wp-content/debug.log something that looks like

[16-Apr-2022 17:13:19 UTC] Page: your-domain.com; option: elementor_version; file: wp-content/plugins/elementor/core/upgrade/manager.php
[16-Apr-2022 17:13:19 UTC] Page: your-domain.com; option: elementor_version; file: wp-content/plugins/elementor/core/base/db-upgrades-manager.php
[16-Apr-2022 17:13:21 UTC] Page: your-domain.com/?wc-ajax=get_refreshed_fragments; option: elementor_version; file: wp-content/plugins/elementor/core/upgrade/manager.php
[16-Apr-2022 17:13:21 UTC] Page: your-domain.com/?wc-ajax=get_refreshed_fragments; option: elementor_version; file: wp-content/plugins/elementor/core/base/db-upgrades-manager.php

 

In this case, we already knew Elementor was the plugin that called that option, but even if we didn’t know it, we would have discovered looking at the file wp-content/debug.log

This method is very useful if you are investigating the autoloaded options and you want to get rid of them.

Many times even though you have deleted a certain plugin, its bloated autoloaded options are still in the database, and you are not sure if you can remove them. Now you know how to check which plugin calls a certain option.

If you need to remove the autoload of an option, it will not be enough to do it in PHPMyAdmin if the plugin that saves that option is still active, because every time it saves that option it will add the autoload to that option. But you can get rid of the bloated autoloaded options with Freesoul Deactivate Plugins PRO. Read the documentation for more details: