Outil de recherche WordPress pour les développeurs et auteurs de thèmes



maybe_add_column ›

Depuis1.0.0
Dépréciéen/a
maybe_add_column ( $table_name, $column_name, $create_ddl )
Paramètres: (3)
  • (string) $table_name Database table name
    Requis: Oui
  • (string) $column_name Table column name
    Requis: Oui
  • (string) $create_ddl SQL to add column to table.
    Requis: Oui
Retourne:
  • (bool) False on failure. True, if already exists or was successful.
Défini(e) dans:
Codex:

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



Source

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;
	}
endif;