从数据库表中删除列(如果存在)。

原型

maybe_drop_column( string $table_name, string $column_name, string $drop_ddl )

参数

$table_name (string) (Required) 表名

$column_name (string) (Required) 列名

$drop_ddl (string) (Required) 要删除列的SQL语句。

返回值

(bool)  失败时为假,成功时为真或不存在。

源文件

路径:wp-admin/install-helper.php

<?php
...
function maybe_drop_column($table_name, $column_name, $drop_ddl) {
	global $wpdb;
	foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
		if ($column == $column_name) {
			// Found it, so try to drop it.
			$wpdb->query($drop_ddl);
			// We cannot directly tell that whether this succeeded!
			foreach ($wpdb->get_col("DESC $table_name",0) as $column ) {
				if ($column == $column_name) {
					return false;
				}
			}
		}
	}
	// Else didn't find it.
	return true;
}
...
?>

其他

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