WordPress at a glance
function is not described

maybe_add_column() WP 1.0.0

Add column to database table, if column doesn't already exist in table.

  • Global. wpdb. $wpdb WordPress database abstraction object.

No Hooks.

Return

true/false. False on failure. True, if already exists or was successful.

Usage

maybe_add_column( $table_name, $column_name, $create_ddl );
$table_name(string) (required)
Database table name
$column_name(string) (required)
Table column name
$create_ddl(string) (required)
SQL to add column to table.

Code of maybe add column: wp-admin/install-helper.php WP 5.2.2

<?php
function maybe_add_column( $table_name, $column_name, $create_ddl ) {
	global $wpdb;
	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.
	$wpdb->query( $create_ddl );

	// We cannot directly tell that whether this succeeded!
	foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
		if ( $column == $column_name ) {
			return true;
		}
	}
	return false;
}