如果列尚不存在,则将列添加到数据库表。

原型

maybe_add_column( string $table_name, string $column_name, string $create_ddl )

参数

$table_name (string) (Required) 要修改的表名。

$column_name (string) (Required) 要添加到表中的列名称。

$create_ddl (string) (Required) 用于添加列的SQL语句。

返回值

(bool)  如果已存在或成功完成,则为True,错误时为false。

源文件

路径:wp-admin/includes/upgrade.php

<?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 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;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/maybe_add_column/