dbDelta sql database (ok)

https://developer.wordpress.org/reference/functions/dbdelta/

private function index_test_001() {
     global $wpdb;
     $table_name = $wpdb->prefix . 'dbdelta_test_001';
     $wpdb_collate = $wpdb->collate;
     $sql =
         "CREATE TABLE {$table_name} (
         id mediumint(8) unsigned NOT NULL auto_increment ,
         first varchar(255) NULL,
         PRIMARY KEY  (id),
         KEY first (first)
         )
         COLLATE {$wpdb_collate}";
 
     require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
     dbDelta( $sql );
 }

Hoặc

public function lionel_content_db_install () {
    global $wpdb;
    $charset_collate = $wpdb->get_charset_collate ();
    $this->lionel_content_set_wpdb_tables ();
    require_once ( ABSPATH . 'wp-admin/includes/upgrade.php' );
    $sql = "CREATE TABLE IF NOT EXISTS $wpdb->lionel_content_logs (
        `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
        `post_id` int(11) NOT NULL,
        `action` enum('publish','update','repost','bulk_publish', 'share', 'add_queue') DEFAULT NULL,
        `request_sent` datetime NOT NULL,
        `profile_id` varchar(191) NOT NULL,
        `profile_name` varchar(191) NOT NULL DEFAULT '',
        `result` enum('success','test','pending','warning','error') NOT NULL DEFAULT 'success',
        `result_message` text,
        `status_text` text,
        `status_image` text,
        `status_link` text,
        `status_created_at` datetime DEFAULT NULL,
        `status_due_at` datetime DEFAULT NULL,
        `lionel_posting_schedule` text,
        PRIMARY KEY (`id`),
        KEY `post_id` (`post_id`),
        KEY `action` (`action`),
        KEY `result` (`result`),
        KEY `profile_id` (`profile_id`)
      ) {$charset_collate};";
    dbDelta ($sql);
      $sqlCate = "CREATE TABLE IF NOT EXISTS $wpdb->lionel_categories (
        `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
        `name` varchar(191) NOT NULL DEFAULT '',
        `description` text,
        `random_availability` int(1) NOT NULL DEFAULT 1,
        `status` int(1) NOT NULL DEFAULT 1,
        `is_default` int(1) DEFAULT NULL,
        `created_at` datetime DEFAULT NULL,
        `updated_at` datetime DEFAULT NULL,
        PRIMARY KEY (`id`)
      ) {$charset_collate};";
    dbDelta ( $sqlCate );
    $categories = $wpdb->get_results ( "SELECT * FROM {$wpdb->lionel_categories}" );
    if(empty((array)$categories)) {
      $dateNow = date('Y-m-d H:i:s');
      $insertSql = "INSERT INTO {$wpdb->lionel_categories}(name, description, random_availability, status, is_default, created_at, updated_at) VALUES ('Your evergreen content', 'Use this category to promote your own content - share any blog posts or pages you publish.', 1, 1, 1, '$dateNow', null), ('Promotional', 'Use this category to promote your services/products. Make sure to include Calls To Actions (CTAs) like \"Book a call with us\" or \"start a trial account\".', 1, 1, 0, '$dateNow', null), ('Quotes, Questions, and Fun', 'Use this category to generate engagement on your socials. Share inspirational or fun quotes, from your niche or create open questions for your audience.', 1, 1, 0, '$dateNow', null)";
      dbDelta($insertSql);
    }
  }

Last updated