wppaste
WordPress

dbDelta( string[]|string $queries = '', bool $execute = true ): string[]

Since
1.5.0, 6.1.0
Source
wp-admin/includes/upgrade.php:2936
Modifies the database based on specified SQL statements.

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

string[]
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

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
46–51

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

Plugin surface
3 hooks

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.

Called by
3

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

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
!$queries && is_array($queries)46–51apply_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

PHPCompiledExecutedBranchesNotes
8.6-dev57746–5168
8.557746–5168
8.457746–516837 fewer instructions than PHP 8.3
8.361446–5168
8.261446–5168
8.161446–51681 fewer instruction than PHP 7.4
7.461546–5168

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:

  1. apply_filters( dbdelta_queries )filterline 2956 (+20 into the body)

    Filters the dbDelta SQL queries.

  2. apply_filters( dbdelta_create_queries )filterline 2997 (+61 into the body)

    Filters the dbDelta SQL queries for creating tables and/or databases.

  3. apply_filters( dbdelta_insert_queries )filterline 3008 (+72 into the body)

    Filters the dbDelta SQL queries for inserting or updating.

Uses · 4

Used by · 3

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 ) ) {			$table_name = trim( $matches[1], '`' ); 			$cqueries[ $table_name ]   = $qry;			$for_update[ $table_name ] = '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();

Changelog

Introduced in 1.5.0. One change between 6.7.7 and 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.8.8
Return type changed from array to string[].verified against source
6.1.0
Ignores display width for integer data types on MySQL 8.0.17 or later, to match MySQL behavior. Note: This does not affect MariaDB.from the docblock
1.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.