Title: maybe_add_column
Published: April 25, 2014
Last modified: August 20, 2026

---

# maybe_add_column( string $table_name, string $column_name, string $create_ddl ): bool

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/maybe_add_column/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/maybe_add_column/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/maybe_add_column/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/maybe_add_column/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/maybe_add_column/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/maybe_add_column/?output_format=md#wp--skip-link--target)

Adds column to database table, if it doesn’t already exist.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/maybe_add_column/?output_format=md#parameters)󠁿

 `$table_name`stringrequired

Database table name.

`$column_name`stringrequired

Table column name.

`$create_ddl`stringrequired

SQL statement to add column.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/maybe_add_column/?output_format=md#return)󠁿

 bool True on success or if the column already exists. False on failure.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/maybe_add_column/?output_format=md#source)󠁿

    ```php
    function maybe_add_column( $table_name, $column_name, $create_ddl ) {
    	global $wpdb;

    	// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Cannot be prepared. Fetches columns for table names.
    	foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
    		if ( $column === $column_name ) {
    			return true;
    		}
    	}

    	// Didn't find it, so try to create it.
    	// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- No applicable variables for this query.
    	$wpdb->query( $create_ddl );

    	// We cannot directly tell whether this succeeded!
    	// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Cannot be prepared. Fetches columns for table names.
    	foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
    		if ( $column === $column_name ) {
    			return true;
    		}
    	}

    	return false;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/install-helper.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-admin/install-helper.php#L91)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-admin/install-helper.php#L91-L114)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/maybe_add_column/?output_format=md#related)󠁿

| Uses | Description | 
| [wpdb::get_col()](https://developer.wordpress.org/reference/classes/wpdb/get_col/)`wp-includes/class-wpdb.php` |

Retrieves one column from the database.

  | 
| [wpdb::query()](https://developer.wordpress.org/reference/classes/wpdb/query/)`wp-includes/class-wpdb.php` |

Performs a database query, using current database connection.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/maybe_add_column/?output_format=md#changelog)󠁿

| Version | Description | 
| [1.0.0](https://developer.wordpress.org/reference/since/1.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fmaybe_add_column%2F)
before being able to contribute a note or feedback.