Skip to content

Instantly share code, notes, and snippets.

@atomjoy
Last active September 15, 2024 19:26
Show Gist options
  • Save atomjoy/d4d39c1ae75275bace87afbe414d89bf to your computer and use it in GitHub Desktop.
Save atomjoy/d4d39c1ae75275bace87afbe414d89bf to your computer and use it in GitHub Desktop.
Wordpress custon database table.
<?php
function wp_setup_translations_table(){
global $wpdb;
$table_name = $wpdb->prefix . "translations"; //get the database table prefix to create my new table
$sql = "CREATE TABLE $table_name (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
identifier varchar(255) NOT NULL,
translation varchar(255) NOT NULL,
lang varchar(5) NOT NULL,
notes varchar(255) DEFAULT NULL,
PRIMARY KEY (id),
KEY Index2 (lang),
KEY Index3 (identifier)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"; // better use: utf8mb4_0900_ai_ci
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
// Create subscribers table
function wp_setup_subscribers_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'subscribers';
$sql = "CREATE TABLE $table_name (
id bigint(21) NOT NULL AUTO_INCREMENT,
email varchar (190) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"; // better use: utf8mb4_0900_ai_ci
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
// In function
add_action('init', 'wp_setup_subscribers_table');
// In theme
add_action('after_switch_theme', 'wp_setup_subscribers_table');
// In plugin
register_activation_hook(__FILE__, 'wp_setup_subscribers_table');
/**
* Register the REST API wp-learn-form-submissions-api/v1/form-submission routes
*/
add_action( 'rest_api_init', 'wp_learn_register_routes' );
function wp_learn_register_routes() {
// Register the routes
// GET https://example.test/wp-json/wp-learn-form-submissions-api/v1/form-submissions
register_rest_route(
'wp-learn-form-submissions-api/v1',
'/form-submissions/',
array(
'methods' => 'GET',
'callback' => 'wp_learn_get_form_submissions',
'permission_callback' => '__return_true',
// 'permission_callback' => 'wp_learn_require_permissions'
)
);
// POST https://example.test/wp-json/wp-learn-form-submissions-api/v1/form-submissions
register_rest_route(
'wp-learn-form-submissions-api/v1',
'/form-submission/',
array(
'methods' => 'POST',
'callback' => 'wp_learn_create_form_submission',
'permission_callback' => '__return_true',
// 'permission_callback' => 'wp_learn_require_permissions'
)
);
// GET single https://example.test/wp-json/wp-learn-form-submissions-api/v1/form-submission/1
register_rest_route(
'wp-learn-form-submissions-api/v1',
'/form-submission/(?P<id>\d+)',
array(
'methods' => 'GET',
'callback' => 'wp_learn_rest_get_form_submission',
'permission_callback' => '__return_true',
// 'permission_callback' => 'wp_learn_require_permissions'
)
);
}
/**
* GET callback for the wp-learn-form-submissions-api/v1/form-submission route
*
* @return array|object|stdClass[]|null
*/
function wp_learn_get_form_submissions() {
global $wpdb;
$table_name = $wpdb->prefix . 'form_submissions';
$results = $wpdb->get_results( "SELECT * FROM $table_name" );
return $results;
}
// GET https://example.test/wp-json/wp-learn-form-submissions-api/v1/form-submission/1
function wp_learn_rest_get_form_submission( $request ) {
$id = $request['id'];
global $wpdb;
$table_name = $wpdb->prefix . 'form_submissions';
$results = $wpdb->get_results( "SELECT * FROM $table_name WHERE id = $id" );
return $results[0];
}
/**
* POST callback for the wp-learn-form-submissions-api/v1/form-submission route
*
* @param $request
*
* @return void
*/
function wp_learn_create_form_submission( $request ){
global $wpdb;
$table_name = $wpdb->prefix . 'form_submissions';
$rows = $wpdb->insert(
$table_name,
array(
'name' => $request['name'],
'email' => $request['email'],
)
);
return $rows;
}
// Check user permissions
function wp_learn_require_permissions() {
return current_user_can( 'edit_posts' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment