1

Does anyone know how to get all the functions in the current plugin? Only those functions which it declared?

I know about function get_defined_functions() and tried it, but this function gets list of all function, but I needed only in current plugin. Maybe in WP there is the function which can get all functions in plugin?

Of course we can get the names of the function in the following way, but it is not the best way, because our plugin can include other files and I can't get their functions.

$filename = __FILE__;
$matches = array();
preg_match_all('/function\s+(\w*)\s*\(/', file_get_contents($filename), $matches);
$matches = $matches[1];
2
  • The function in your link get function only in current file. Current file can include many other files! And this function never get array with all functions. I use WP. which add plugin when they install and have its space. I need get list of functions for all plugin, not part of it's. Commented Mar 19, 2014 at 14:06
  • Yes, sorry, I misread that part :/ I'll retract my close vote; the upvote is mine btw. That function is part of the solution, because I'm getting an array with the list of all functions: $arr = get_defined_functions_in_file( plugin_dir_path( __FILE__ ) ); var_dump ($arr); Commented Mar 19, 2014 at 14:11

1 Answer 1

2

The following code is based on this Q&A's:

It's just a test and it will dump all PHP files and its functions in every WP page (front and backend). The code looks for all PHP files inside the current plugin directory and search for each file functions.

add_action('plugins_loaded', function() {
    $path = plugin_dir_path( __FILE__ );
    $it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path ) );
    foreach ($it as $file) {
        $ext = pathinfo( $file, PATHINFO_EXTENSION );
        if( 'php' === $ext ) {
            echo "<br><br>".$file."<br>";
            $arr = get_defined_functions_in_file( $file );
            var_dump ($arr);
        }
    }
});


/* From https://stackoverflow.com/a/2197870 */
function get_defined_functions_in_file($file) {
    $source = file_get_contents($file);
    $tokens = token_get_all($source);

    $functions = array();
    $nextStringIsFunc = false;
    $inClass = false;
    $bracesCount = 0;

    foreach($tokens as $token) {
        switch($token[0]) {
            case T_CLASS:
                $inClass = true;
                break;
            case T_FUNCTION:
                if(!$inClass) $nextStringIsFunc = true;
                break;

            case T_STRING:
                if($nextStringIsFunc) {
                    $nextStringIsFunc = false;
                    $functions[] = $token[1];
                }
                break;

            // Anonymous functions
            case '(':
            case ';':
                $nextStringIsFunc = false;
                break;

            // Exclude Classes
            case '{':
                if($inClass) $bracesCount++;
                break;

            case '}':
                if($inClass) {
                    $bracesCount--;
                    if($bracesCount === 0) $inClass = false;
                }
                break;
        }
    }

    return $functions;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great! For me it would be enough! Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.