wppaste
WordPress

wpdb::has_cap( string $db_cap ): bool

Since
2.7.0, 4.1.0, 4.6.0, 6.2.0, 6.6.0
Source
wp-includes/class-wpdb.php:4078
Determines whether the database or WPDB supports a particular feature.

Description

Capability sniffs for the database server and current version of WPDB.

Database sniffs are based on the version of the database server in use.

WPDB sniffs are added as new features are introduced to allow theme and plugin developers to determine feature support. This is to account for drop-ins which may introduce feature support at a different time to WordPress.

Compatibility

WordPress
since 6.6.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

$db_capstring
The feature to check for. Accepts 'collation', 'group_concat', 'subqueries', 'set_charset', 'utf8mb4', 'utf8mb4_520', or 'identifier_placeholders'.

Return value

bool
True when the database feature is supported, false otherwise.

Performance profile

How much work a call to wpdb::has_cap() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
15–33

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
3

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

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wpdb::has_cap() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always15–31->db_version(), ->db_server_info(), strtolower()
always20–33->db_version(), ->db_server_info(), strtolower(), version_compare()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5615–339
8.55615–339
8.45615–3393 fewer instructions than PHP 8.3
8.35915–369
8.25915–3691 more instruction than PHP 8.1
8.15815–38914 fewer instructions than PHP 7.4
7.47215–5210

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 · 2

Used by · 3

Source code

	public function has_cap( $db_cap ) {		$db_version     = $this->db_version();		$db_server_info = $this->db_server_info(); 		/*		 * Account for MariaDB version being prefixed with '5.5.5-' on older PHP versions.		 *		 * Note: str_contains() is not used here, as this file can be included		 * directly outside of WordPress core, e.g. by HyperDB, in which case		 * the polyfills from wp-includes/compat.php are not loaded.		 */		if ( '5.5.5' === $db_version && false !== strpos( $db_server_info, 'MariaDB' )			&& PHP_VERSION_ID < 80016 // PHP 8.0.15 or older.		) {			// Strip the '5.5.5-' prefix and set the version to the correct value.			$db_server_info = preg_replace( '/^5\.5\.5-(.*)/', '$1', $db_server_info );			$db_version     = preg_replace( '/[^0-9.].*/', '', $db_server_info );		} 		switch ( strtolower( $db_cap ) ) {			case 'collation':    // @since 2.5.0			case 'group_concat': // @since 2.7.0			case 'subqueries':   // @since 2.7.0				return version_compare( $db_version, '4.1', '>=' );			case 'set_charset':				return version_compare( $db_version, '5.0.7', '>=' );			case 'utf8mb4':      // @since 4.1.0				return true;			case 'utf8mb4_520': // @since 4.6.0				return version_compare( $db_version, '5.6', '>=' );			case 'identifier_placeholders': // @since 6.2.0				/*				 * As of WordPress 6.2, wpdb::prepare() supports identifiers via '%i',				 * e.g. table/field names.				 */				return true;		} 		return false;	}

Changelog

Introduced in 2.7.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.

6.6.0
The utf8mb4 feature now always returns true.from the docblock
6.2.0
Added support for the 'identifier_placeholders' feature.from the docblock
4.6.0
Added support for the 'utf8mb4_520' feature.from the docblock
4.1.0
Added support for the 'utf8mb4' feature.from the docblock
2.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/class-wpdb.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.