Using WordPress ‘maybe_drop_column()’ PHP function

The maybe_drop_column WordPress PHP function drops a column from a database table if it exists.

Usage

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

Parameters

  • $table_name (string) – Required. Database table name.
  • $column_name (string) – Required. Table column name.
  • $drop_ddl (string) – Required. SQL statement to drop the column.

More information

See WordPress Developer Resources: maybe_drop_column

Examples

Drop ’email’ column from ‘users’ table

Dropping the ’email’ column from the ‘users’ table.

global $wpdb;
$table_name = $wpdb->prefix . 'users';
$column_name = 'email';
$drop_ddl = "ALTER TABLE $table_name DROP COLUMN $column_name;";
maybe_drop_column($table_name, $column_name, $drop_ddl);

Drop ‘age’ column from ‘members’ table

Dropping the ‘age’ column from the ‘members’ table.

global $wpdb;
$table_name = $wpdb->prefix . 'members';
$column_name = 'age';
$drop_ddl = "ALTER TABLE $table_name DROP COLUMN $column_name;";
maybe_drop_column($table_name, $column_name, $drop_ddl);

Drop ‘phone’ column from ‘contacts’ table

Dropping the ‘phone’ column from the ‘contacts’ table.

global $wpdb;
$table_name = $wpdb->prefix . 'contacts';
$column_name = 'phone';
$drop_ddl = "ALTER TABLE $table_name DROP COLUMN $column_name;";
maybe_drop_column($table_name, $column_name, $drop_ddl);

Drop ‘location’ column from ‘events’ table

Dropping the ‘location’ column from the ‘events’ table.

global $wpdb;
$table_name = $wpdb->prefix . 'events';
$column_name = 'location';
$drop_ddl = "ALTER TABLE $table_name DROP COLUMN $column_name;";
maybe_drop_column($table_name, $column_name, $drop_ddl);

Drop ‘description’ column from ‘products’ table

Dropping the ‘description’ column from the ‘products’ table.

global $wpdb;
$table_name = $wpdb->prefix . 'products';
$column_name = 'description';
$drop_ddl = "ALTER TABLE $table_name DROP COLUMN $column_name;";
maybe_drop_column($table_name, $column_name, $drop_ddl);