Find the file where the given function is defined

In single-app, micro-service development almost all functions belong to you or to your team, so no need to know who defined the given function – you probably know it already.

But in CMS development some you may want to find the file where the given function is defined.

It might be WP core, template functions, theme or any of active plugins.

To find it you can use built-in PHP class called “ReflectionFunction”.

For example create such simple hook.

add_action('wp_head',function(){
    if(isset($_GET["find_my_function"])){
        $funcname='get_custom_name';
        $reflection=new ReflectionFunction($funcname);
        $path=$reflection->getFileName();
        echo 'The function '.$funcname.' is defined in:'.$path;
        die();
    }
});

After adding this block to functions.php of your theme, just go the browser and call

yourwebsite.com/?find_my_function=1

That’s all, then view-source of the page in order to see it clearly, and you will probably see some string like this:

The function “get_custom_name” is defined in : /var/www/html/wp-content/plugins/custom-plugin-name/index.php.

This code can tell you where any PHP function is defined in your WordPress website.

After that, don’t forget to delete the code from your functions.php.

Get more useful WP tricks and snippets by subscribing to my mailclub.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.