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:4061
Description
Capability sniffs for the database server and current version of WPDB.
Database sniffs are based on the version of MySQL the site is using.
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
- Scaling
- Constant
- Instructions
- 15–33
- Plugin surface
- None
- Called by
- 3
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 56.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 15–31 | ->db_version(), ->db_server_info(), strtolower() |
| always | 20–33 | ->db_version(), ->db_server_info(), strtolower(), version_compare() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 56 | 15–33 | 9 | |
| 8.5 | 56 | 15–33 | 9 | |
| 8.4 | 56 | 15–33 | 9 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 59 | 15–36 | 9 | |
| 8.2 | 59 | 15–36 | 9 | 1 more instruction than PHP 8.1 |
| 8.1 | 58 | 15–38 | 9 | 14 fewer instructions than PHP 7.4 |
| 7.4 | 72 | 15–52 | 10 |
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
- wpdb::db_version()Retrieves the database server version.
- wpdb::db_server_info()Returns the version of the MySQL server.
Used by · 3
- wpdb::determine_charset()Determines the best charset and collation to use given a charset and collation.
- wpdb::set_charset()Sets the connection's character set.
- wpdb::supports_collation()Determines whether the database supports collation.
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.
Signature, return type and hooks compared across 5 parsed releases.
utf8mb4 feature now always returns true.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.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.