Created
May 6, 2024 18:11
-
-
Save MaximilianoRicoTabo/3d895c4e468466b0cac1ed3569803281 to your computer and use it in GitHub Desktop.
Check if required tables exist and create them if they don't.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Check if required tables exist and create them if they don't. | |
* This function is used to catch up missing tables that should have been created by Paid Memberships Pro when update to v3 | |
* | |
* link: TBD | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function pmpro_catch_up_missing_tables() { | |
global $wpdb; | |
//This may be modified to include other tables as needed | |
$missed_tables = array( | |
"{$wpdb->prefix}pmpro_subscriptions", | |
"{$wpdb->prefix}pmpro_subscriptionmeta", | |
"{$wpdb->prefix}pmpro_groups", | |
"{$wpdb->prefix}pmpro_membership_levels_groups" | |
); | |
// Get all table names from the database | |
$all_tables = $wpdb->get_col( "SHOW TABLES" ); | |
// Check if all required tables exist | |
foreach ( $missed_tables as $table_name ) { | |
if ( !in_array( $table_name, $all_tables ) ) { | |
// Table is missing, call the dbDelta function to create it | |
pmpro_db_delta(); | |
break; | |
} | |
} | |
} | |
add_filter('admin_init', 'pmpro_catch_up_missing_tables'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment