Where should I be putting this function to create a new table in for my Wordpress site? I am currently putting it in the wp_content/themes/theme/function.php file but I am not sure this is the correct place as I am not seeing it in phpMyAdmin after saving.
This is the function:
function mental_health_providers_create_db() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
//* Create the table
$table_name = $wpdb->prefix . 'mental_health_providers';
$sql = "CREATE TABLE $table_name (
provider_id INTEGER NOT NULL AUTO_INCREMENT,
provider_name TEXT NOT NULL,
provider_city TEXT NOT NULL,
provider_phone TEXT NOT NULL,
PRIMARY KEY (provider_id)
) $charset_collate;";
dbDelta( $sql );
}
register_activation_hook( __FILE__, 'mental_health_providers_create_db' );
Where should I be putting this function to create a new table in for my Wordpress site? I am currently putting it in the wp_content/themes/theme/function.php file but I am not sure this is the correct place as I am not seeing it in phpMyAdmin after saving.
This is the function:
function mental_health_providers_create_db() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
//* Create the table
$table_name = $wpdb->prefix . 'mental_health_providers';
$sql = "CREATE TABLE $table_name (
provider_id INTEGER NOT NULL AUTO_INCREMENT,
provider_name TEXT NOT NULL,
provider_city TEXT NOT NULL,
provider_phone TEXT NOT NULL,
PRIMARY KEY (provider_id)
) $charset_collate;";
dbDelta( $sql );
}
register_activation_hook( __FILE__, 'mental_health_providers_create_db' );
Share
Improve this question
edited Jul 1, 2020 at 1:10
mozboz
2,6281 gold badge12 silver badges23 bronze badges
asked Jun 30, 2020 at 23:14
Vera KVera K
1
1
- 1 register_activation_hook is for plugins and makes the code run when the plugin is activated. Are you writing a plugin? – mozboz Commented Jul 1, 2020 at 0:28
1 Answer
Reset to default 0The code you are currently using for being used as a plugin. register_activation_hook
is reserved for plugin use only. It only fires when plugin in activated. If you want to create database table from your theme you can use after_switch_theme
. It can be used like bellow:
add_action("after_switch_theme", "mental_health_providers_create_db");
function mental_health_providers_create_db() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
//* Create the table
$table_name = $wpdb->prefix . 'mental_health_providers';
$sql = "CREATE TABLE $table_name (
provider_id INTEGER NOT NULL AUTO_INCREMENT,
provider_name TEXT NOT NULL,
provider_city TEXT NOT NULL,
provider_phone TEXT NOT NULL,
PRIMARY KEY (provider_id)
) $charset_collate;";
dbDelta( $sql );
}
Again I'll suggest you to write a simple plugin to handle the database creation part. Read here on how to write a simple plugin
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742302492a4418291.html
评论列表(0条)