dbDelta( string[]|string $queries = '', bool $execute = true ): array
- Since
- 1.5.0, 6.1.0
- Source
wp-admin/includes/upgrade.php:2856
Description
Useful for creating new tables and updating existing tables to a new structure.
Compatibility
- WordPress
- since 6.1.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
$queriesstring[]|stringoptional- The query to run. Can be multiple queries in an array, or a string of queries separated by semicolons. Default empty string.Default:
'' $executebooloptional- Whether or not to execute the query right away.
Default true.Default:true
Return value
array- Strings containing the results of the various update queries.
Performance profile
How much work a call to dbDelta() 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
- Light
- Scaling
- Scales with input
- Instructions
- 46–51
- Plugin surface
- 3 hooks
- Called by
- 3
Touches nothing outside its own arguments.
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 576.
Third-party callbacks on 'dbdelta_queries', 'dbdelta_create_queries', 'dbdelta_insert_queries' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost dbDelta() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$queries && is_array($queries) | 46–51 | apply_filters(), apply_filters(), apply_filters(), ->tables(), ->db_version(), ->db_server_info(), array_merge() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 576 | 46–51 | 68 | |
| 8.5 | 576 | 46–51 | 68 | |
| 8.4 | 576 | 46–51 | 68 | 37 fewer instructions than PHP 8.3 |
| 8.3 | 613 | 46–51 | 68 | |
| 8.2 | 613 | 46–51 | 68 | |
| 8.1 | 613 | 46–51 | 68 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 614 | 46–51 | 68 |
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.
Hooks and filters fired · 3
3 hooks fire while dbDelta() runs, in this order:
- apply_filters( dbdelta_create_queries )filterline 2915 (+59 into the body)
Filters the dbDelta SQL queries for creating tables and/or databases.
- apply_filters( dbdelta_insert_queries )filterline 2926 (+70 into the body)
Filters the dbDelta SQL queries for inserting or updating.
Uses · 4
- wp_get_db_schema()Retrieve the SQL for creating database tables.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_should_upgrade_global_tables()Determine if global tables should be upgraded.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
Used by · 3
- install_network()Install Network.
- make_db_current()Updates the database tables to a new schema.
- make_db_current_silent()Updates the database tables to a new schema, but without displaying results.
Source code
function dbDelta( $queries = '', $execute = true ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid global $wpdb; if ( in_array( $queries, array( '', 'all', 'blog', 'global', 'ms_global' ), true ) ) { $queries = wp_get_db_schema( $queries ); } // Separate individual queries into an array. if ( ! is_array( $queries ) ) { $queries = explode( ';', $queries ); $queries = array_filter( $queries ); } /** * Filters the dbDelta SQL queries. * * @since 3.3.0 * * @param string[] $queries An array of dbDelta SQL queries. */ $queries = apply_filters( 'dbdelta_queries', $queries ); $cqueries = array(); // Creation queries. $iqueries = array(); // Insertion queries. $for_update = array(); // Create a tablename index for an array ($cqueries) of recognized query types. foreach ( $queries as $qry ) { if ( preg_match( '|CREATE TABLE ([^ ]*)|', $qry, $matches ) ) { $cqueries[ trim( $matches[1], '`' ) ] = $qry; $for_update[ $matches[1] ] = 'Created table ' . $matches[1]; continue; } if ( preg_match( '|CREATE DATABASE ([^ ]*)|', $qry, $matches ) ) { array_unshift( $cqueries, $qry ); continue; } if ( preg_match( '|INSERT INTO ([^ ]*)|', $qry, $matches ) ) { $iqueries[] = $qry; continue; } if ( preg_match( '|UPDATE ([^ ]*)|', $qry, $matches ) ) { $iqueries[] = $qry; continue; } } /** * Filters the dbDelta SQL queries for creating tables and/or databases. * * Queries filterable via this hook contain "CREATE TABLE" or "CREATE DATABASE". * * @since 3.3.0 * * @param string[] $cqueries An array of dbDelta create SQL queries. */ $cqueries = apply_filters( 'dbdelta_create_queries', $cqueries ); /** * Filters the dbDelta SQL queries for inserting or updating. * * Queries filterable via this hook contain "INSERT INTO" or "UPDATE". * * @since 3.3.0 * * @param string[] $iqueries An array of dbDelta insert or update SQL queries. */ $iqueries = apply_filters( 'dbdelta_insert_queries', $iqueries ); $text_fields = array( 'tinytext', 'text', 'mediumtext', 'longtext' ); $blob_fields = array( 'tinyblob', 'blob', 'mediumblob', 'longblob' ); $int_fields = array( 'tinyint', 'smallint', 'mediumint', 'int', 'integer', 'bigint' ); $global_tables = $wpdb->tables( 'global' ); $db_version = $wpdb->db_version(); $db_server_info = $wpdb->db_server_info();Changelog
Introduced in 1.5.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
array to string[].verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-admin/includes/upgrade.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.