wppaste
WordPress

_get_plugin_from_callback( callable $callback ): array|null

Since
5.0.0
Source
wp-admin/includes/template.php:1244
Internal helper function to find the plugin from a meta box callback.

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

Only touches the object cache, via wp_cache_get().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
14–45

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 63.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • cacheobject cachewp_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.

WhenInstructionsCalls it makes
->isInternal()14–17->isInternal()
!->isInternal() && !$filename27–30->isInternal(), ->getFileName(), wp_normalize_path(), wp_normalize_path()
!->isInternal() && $filename38–45->isInternal(), ->getFileName(), wp_normalize_path(), wp_normalize_path(), get_plugins()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev6114–4482 fewer instructions than PHP 8.5
8.56314–458
8.46314–45815 fewer instructions than PHP 8.3
8.37814–598
8.27814–598
8.17814–598
7.47814–598

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

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.