database - Where do I put my create_new_table function()?

Where should I be putting this function to create a new table in for my Wordpress site? I am currently putting it in the

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
Add a comment  | 

1 Answer 1

Reset to default 0

The 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

相关推荐

  • database - Where do I put my create_new_table function()?

    Where should I be putting this function to create a new table in for my Wordpress site? I am currently putting it in the

    16小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信