Skip to content

Instantly share code, notes, and snippets.

@alordiel
Last active May 5, 2018 07:40
Show Gist options
  • Save alordiel/75fd79b5c9951994e331447e04216018 to your computer and use it in GitHub Desktop.
Save alordiel/75fd79b5c9951994e331447e04216018 to your computer and use it in GitHub Desktop.
Adding a new column to the custom database table in WordPress
<?php
function add_new_columns_to_easyexam_students_exams () {
global $wpdb;
$table_name = $wpdb->prefix . 'table_name';
// Check if column exists
$column_exist = $wpdb->query("SHOW COLUMNS FROM $table_name LIKE 'column_name'");
if ($column_exist == false) {
// alter the table with the new column name
$wpdb->query("ALTER TABLE $table ADD column_name VARCHAR(255) NOT NULL AFTER some_other_column_name");
}
}
// Hook on plugin's activation and execute the above function
register_activation_hook( __FILE__, 'plugin_prefix_database_function' );
function plugin_prefix_database_function () {
add_new_columns_to_easyexam_students_exams ();
// and here you can add some more function that should be executed only once
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment