maybe_drop_column

Drop column from database table, if it exists.

Description

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

Parameters (3)

0. $table_name (string)
The table name.
1. $column_name (string)
The column name.
2. $drop_ddl (string)
SQL statement to drop column.

Usage

  1. if ( !function_exists( 'maybe_drop_column' ) ) { 
  2. require_once ABSPATH . '/wp-admin/install-helper.php'; 
  3.  
  4. // The table name. 
  5. $table_name = ''; 
  6.  
  7. // The column name. 
  8. $column_name = ''; 
  9.  
  10. // SQL statement to drop column. 
  11. $drop_ddl = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = maybe_drop_column($table_name, $column_name, $drop_ddl); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/install-helper.php  
  1. function maybe_drop_column($table_name, $column_name, $drop_ddl) { 
  2. global $wpdb; 
  3. foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) { 
  4. if ($column == $column_name) { 
  5.  
  6. // Found it, so try to drop it. 
  7. $wpdb->query($drop_ddl); 
  8.  
  9. // We cannot directly tell that whether this succeeded! 
  10. foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) { 
  11. if ($column == $column_name) { 
  12. return false; 
  13. // Else didn't find it. 
  14. return true;