_get_plugin_from_callback( callable $callback ): array|null
- Since
- 5.0.0
- Source
wp-admin/includes/template.php:1244
Compatibility
- WordPress
- since 5.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$callbackcallable- The callback function to check.
Return value
array|null- The plugin that the callback belongs to, or null if it doesn't belong to a plugin.
Performance profile
How much work a call to _get_plugin_from_callback() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Moderate
- Scaling
- Scales with input
- Instructions
- 14–45
- Plugin surface
- None
- Called by
- 2
Only touches the object cache, via wp_cache_get().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 63.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_get()one call below _get_plugin_from_callback()
Further down the call graph this can also reach hook, query, option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _get_plugin_from_callback() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
->isInternal() | 14–17 | ->isInternal() |
!->isInternal() && !$filename | 27–30 | ->isInternal(), ->getFileName(), wp_normalize_path(), wp_normalize_path() |
!->isInternal() && $filename | 38–45 | ->isInternal(), ->getFileName(), wp_normalize_path(), wp_normalize_path(), get_plugins() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 61 | 14–44 | 8 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 63 | 14–45 | 8 | |
| 8.4 | 63 | 14–45 | 8 | 15 fewer instructions than PHP 8.3 |
| 8.3 | 78 | 14–59 | 8 | |
| 8.2 | 78 | 14–59 | 8 | |
| 8.1 | 78 | 14–59 | 8 | |
| 7.4 | 78 | 14–59 | 8 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 6
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wp_normalize_path()Normalizes a filesystem path.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- get_plugins()Checks the plugins directory and retrieve all plugin files with plugin data.
- ReflectionMethod::__construct()
- ReflectionFunction::__construct()
Used by · 2
- do_block_editor_incompatible_meta_box()Renders a "fake" meta box with an information message, shown on the block editor, when an incompatible meta box is found.
- do_meta_boxes()Meta-Box template function.
Source code
function _get_plugin_from_callback( $callback ) { try { if ( is_array( $callback ) ) { $reflection = new ReflectionMethod( $callback[0], $callback[1] ); } elseif ( is_string( $callback ) && str_contains( $callback, '::' ) ) { $reflection = new ReflectionMethod( $callback ); } else { $reflection = new ReflectionFunction( $callback ); } } catch ( ReflectionException $exception ) { // We could not properly reflect on the callable, so we abort here. return null; } // Don't show an error if it's an internal PHP function. if ( ! $reflection->isInternal() ) { // Only show errors if the meta box was registered by a plugin. $filename = wp_normalize_path( $reflection->getFileName() ); $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR ); if ( str_starts_with( $filename, $plugin_dir ) ) { $filename = str_replace( $plugin_dir, '', $filename ); $filename = preg_replace( '|^/([^/]*/).*$|', '\\1', $filename ); $plugins = get_plugins(); foreach ( $plugins as $name => $plugin ) { if ( str_starts_with( $name, $filename ) ) { return $plugin; } } } } return null;}Changelog
Introduced in 5.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-admin/includes/template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.