You can create a custom table for the data of your WordPress plugin if you hook on the plugin activation. This has the effect that your custom table will be created if you activate the plugin in the WordPress backend. I created a sample code to show you how this works.
../wp/wp-content/plugins/my-plugin/plugin.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php /* Plugin Name: Gallery Plugin URI: http://www.angelcode.de/ Description: My gallery. Version: 1.0 Author: Benny Neugebauer Author URI: https://www.bennyn.de/ License: GPLv2 */ register_activation_hook(__FILE__, 'create_custom_table'); /** * See: https://codex.wordpress.org/Creating_Tables_with_Plugins * @global type $wpdb */ function create_custom_table() { global $wpdb; $table_prefix = "gallery_"; $table_name = $wpdb->prefix . $table_prefix . "albums"; $sql = "CREATE TABLE " . $table_name . " ( id MEDIUMINT(9) NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, path VARCHAR(255) NOT NULL, PRIMARY KEY album_id (id), UNIQUE KEY album_path (path) ) CHARACTER SET utf8 COLLATE utf8_general_ci;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } ?> |
Note:
You can also use $GLOBALS["wpdb"]->prefix;
instead of:
global $wpdb; $wpdb->prefix |