Skip to content

Instantly share code, notes, and snippets.

@delennerd
Created January 13, 2022 17:09
Show Gist options
  • Save delennerd/e8ac64e9fe22cf00a8b0552a63290598 to your computer and use it in GitHub Desktop.
Save delennerd/e8ac64e9fe22cf00a8b0552a63290598 to your computer and use it in GitHub Desktop.
WordPress Custom DB Table
<?php
/**
* Install database tables
*/
public function db_install_recordings()
{
global $wpdb;
$table_name = $wpdb->prefix . "myprefix_mytable";
$charset_collate = $wpdb->get_charset_collate();
$sql_table = "CREATE TABLE $table_name (
id bigint NOT NULL AUTO_INCREMENT,
host_user_id bigint NOT NULL,
course_id bigint NOT NULL,
is_live tinyint(1) DEFAULT 0 NOT NULL,
filename VARCHAR(250) NULL,
datetime VARCHAR(250) DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql_table );
}
<?php
function insert_in_table()
{
$table = $wpdb->prefix . $this->db_table_recordings;
$data = array(
'host_user_id' => $user_id,
'course_id' => $course_id,
'is_recording' => 1,
'filename' => $filename,
'datetime' => $start_time,
);
$insert = $wpdb->insert($table, $data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment