Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active February 21, 2024 21:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save igorbenic/cf263fd168c67754eca4fa67976b39d9 to your computer and use it in GitHub Desktop.
Save igorbenic/cf263fd168c67754eca4fa67976b39d9 to your computer and use it in GitHub Desktop.
Working with Custom Tables in WordPress – Installing and Updating | https://ibenic.com
<?php
/**
* Installer class
*/
namespace MyPlugin;
class Installer {
// ... previous code
/**
* Installation Method.
*/
public static function install() {
global $wpdb;
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
// Example table
$schema = "CREATE TABLE {$wpdb->prefix}myplugin_products (
ID bigint(20) NOT NULL AUTO_INCREMENT,
title varchar(200) NOT NULL,
description longtext NOT NULL,
price tinytext NOT NULL,
quantity int(3) NOT NULL,
status varchar(20) NOT NULL default 'active',
type varchar(20) NOT NULL default 'onetime',
PRIMARY KEY ID (ID)
) CHARACTER SET utf8 COLLATE utf8_general_ci;";
// Example meta
$schema .= "CREATE TABLE {$wpdb->prefix}myplugin_productmeta (
meta_id bigint(20) NOT NULL AUTO_INCREMENT,
myplugin_product_id bigint(20) NOT NULL DEFAULT '0',
meta_key varchar(255) DEFAULT NULL,
meta_value longtext,
PRIMARY KEY meta_id (meta_id),
KEY myplugin_product_id (myplugin_product_id),
KEY meta_key (meta_key)
) CHARACTER SET utf8 COLLATE utf8_general_ci;";
}
}
<?php
/**
* Installer class
*/
namespace MyPlugin;
class Installer {
/**
* DB updates and callbacks that need to be run per version.
*
* @var array
*/
private static $db_updates = array();
/** Example:
private static $db_updates = array(
'1.3.0' => array(
'myplugin_update_130',
),
);
*/
/**
* Get list of DB update callbacks.
*
* @return array
*/
public static function get_db_update_callbacks() {
return self::$db_updates;
}
}
<?php
/**
* Installer class
*/
namespace MyPlugin;
class Installer {
/**
* Update the database
*
* @param string $from From which version are we updating it.
*/
public static function update( $from ) {
self::install();
foreach ( self::get_db_update_callbacks() as $version => $update_callbacks ) {
// If the version from which we update is below the $version, call all update functions
if ( version_compare( $from, $version, '<' ) ) {
foreach ( $update_callbacks as $update_callback ) {
if ( function_exists( $update_callback ) ) {
$update_callback();
}
}
}
}
update_option( 'myplugin_version', MY_PLUGIN_VERSION );
}
}
<?php
/**
* Installer class
*/
namespace MyPlugin;
class Installer {
/**
* Check the version and run the updater is required.
*
* This check is done on all requests and runs if the versions do not match.
*/
public static function check_version() {
if ( ! defined( 'IFRAME_REQUEST' ) && version_compare( get_option( 'myplugin_version', '0.0.1' ), MY_PLUGIN_VERSION, '<' ) ) {
self::update( get_option( 'myplugin_version', '0.0.1' ) );
do_action( 'myplugin_updated' );
}
}
}
<?php
/**
* Installer class
*/
namespace MyPlugin;
class Installer {
// ... previous code
/**
* Uninstall method that is called when plugin is being deleted.
*/
public static function uninstall() {
global $wpdb;
/**
* All custom tables we've made.
*/
$tables = array(
'myplugin_products',
'myplugin_productmeta'
);
foreach( $tables as $table ) {
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}" . $table );
}
delete_option( 'myplugin_version' );
// All other custom options being stored here
// Delete all custom meta in other tables as well.
$postmeta_keys = array(
'_has_db_imported',
'_post_is_synced',
'any_other_custom_meta'
);
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key IN ('" . implode( ",'", $postmeta_keys ) . "')" );
// This will create a query such as DELETE FROM wp_postmeta WHERE meta_key IN ('_has_db_imported', '_post_is_synced', 'any_other_custom_meta' )
}
}
<?php
/**
* Installer class
*/
namespace MyPlugin;
class Installer {
/**
* Method called when a plugin is activated.
*/
public static function activate() {
self::install();
}
/**
* Installation Method.
*/
public static function install() {}
}
<?php
/**
* This represents the main plugin file.
*/
namespace MyPlugin;
class Plugin {
public function __construct() {
global $wpdb;
// Registering meta table
$wpdb->myplugin_productmeta = $wpdb->prefix . 'myplugin_productmeta';
// other code
}
}
<?php
/**
* This represents the main plugin file.
*/
namespace MyPlugin;
class Plugin {
public function __construct() {
// previous code ...
add_action( 'init', array( '\MyPlugin\Installer', 'check_version' ), 5 );
}
}
<?php
/**
* This represents the main plugin file.
*/
namespace MyPlugin;
class Plugin {
public function __construct() {
include 'path_to_installer_class.php';
register_activation_hook( __FILE__, array( '\MyPlugin\Installer', 'activate' ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment