Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Youssef-Mhamdi/d840f2e753fdfe0883be6b55ef1e8234 to your computer and use it in GitHub Desktop.
Save Youssef-Mhamdi/d840f2e753fdfe0883be6b55ef1e8234 to your computer and use it in GitHub Desktop.
Mu Plugin that adds Smush Missing Table to DB to fix the message taht says: Directory smushing requires custom tables and it seems there was an error creating tables. For help, please contact our team on the support forums.
<?php
/**
* Plugin Name: [Smush] - Add smush_dir_images to DB
* Plugin URI: https://premium.wpmudev.org/
* Description: Add Smush Missing Table to DB to fix the message taht says: Directory smushing requires custom tables and it seems there was an error creating tables. For help, please contact our team on the support forums.
* Task: SLS-1383
* Author: Youssef Mhamdi @ WPMUDEV
* Author URI: https://premium.wpmudev.org/forums/profile/mhamdiyoussef/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
return;
}
add_action('admin_init', 'WPMUDEV_Smush_Add_Smush_Missing_Table_smush_dir_images' );
function WPMUDEV_Smush_Add_Smush_Missing_Table_smush_dir_images()
{
if ( ! class_exists( 'WPMUDEV_Smush_Add_Missing_Table_smush_dir_images' ) ) {
class WPMUDEV_Smush_Add_Missing_Table_smush_dir_images
{
public static function get_instance()
{
if (is_null(self::$_instance)) {
self::$_instance = new WPMUDEV_Smush_Add_Missing_Table_smush_dir_images();
}
return self::$_instance;
}
public function __construct()
{
if ( ! defined( 'WP_SMUSH_VERSION' ) ) {
return;
}
$this->add_smush_table();
}
public function add_smush_table()
{
global $wpdb; $table_name = $wpdb->prefix . 'smush_dir_images';
$charset_collate = $wpdb->get_charset_collate();
$table_exist = $wpdb->get_var(
$wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like(
$wpdb->base_prefix . 'smush_dir_images' )
)
); // Db call ok; no-cache ok.
$table_exists = $table_exist ? true : false;
if ($table_exists) {
return;
}
$sql = "CREATE TABLE {$wpdb->base_prefix}smush_dir_images (
id mediumint(9) NOT NULL AUTO_INCREMENT,
path text NOT NULL,
path_hash CHAR(32),
resize varchar(55),
lossy varchar(55),
error varchar(55) DEFAULT NULL,
image_size int(10) unsigned,
orig_size int(10) unsigned,
file_time int(10) unsigned,
last_scan timestamp DEFAULT '0000-00-00 00:00:00',
meta text,
UNIQUE KEY id (id),
UNIQUE KEY path_hash (path_hash),
KEY image_size (image_size)
) $charset_collate;";
// Include the upgrade library to initialize a table.
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta($sql);
}
}
$run = new WPMUDEV_Smush_Add_Missing_Table_smush_dir_images();
}
}
@Nikasa1889
Copy link

Great, thanks!
If you have error with DigitalOceans saying "Unable to create or change a table without a primary key" change the "UNIQUE KEY" at line 72 to "PRIMARY KEY".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment