Last active
April 3, 2021 22:48
-
-
Save curtisnehring/0241c35ee77744137d8adf17c099c480 to your computer and use it in GitHub Desktop.
Code to re-order Wordpress plug-in load order to place a specifc plugin first or last depending on need
This file contains 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 | |
/*** | |
* Load this in your (child)theme's functions.php | |
* Need help? => CodeByCurtis.com | |
**/ | |
// order plugins that are causing issues | |
function cbc_order_plugins() { | |
$active_plugins = get_option('active_plugins'); | |
// array of plugins that need load priorities managed to avoid conflicts | |
$order_sensative_plugins = [ | |
'load_first' => [ | |
'gmail-smtp/main.php' | |
], | |
'load_last' => [ | |
'automatic-domain-changer/auto-domain-change.php', | |
'ultimate-blocks/ultimate-blocks.php', | |
'product-visibility-by-user-role-for-woocommerce/product-visibility-by-user-role-for-woocommerce.php' | |
] | |
]; | |
foreach ($order_sensative_plugins['load_first'] as $plugin) { | |
if ($key = array_search($plugin, $active_plugins)) { | |
// remove plugin from active array | |
array_splice($active_plugins, $key, 1); | |
// add the plugin to the beginning of the active array | |
array_unshift($active_plugins, $plugin); | |
} | |
} | |
foreach ($order_sensative_plugins['load_last'] as $plugin) { | |
if ($key = array_search($plugin, $active_plugins)) { | |
// remove plugin from active array | |
array_splice($active_plugins, $key, 1); | |
// add the plugin to the end of the active array | |
array_push($active_plugins, $plugin); | |
} | |
} | |
// update the active plugins setting/option in the db | |
update_option('active_plugins', $active_plugins); | |
} | |
add_action('activated_plugin', 'cbc_order_plugins'); // run when a plugin is activated | |
add_action('upgrader_process_complete', 'cbc_order_plugins', 15); // run when a plugin is updated; needed because plugin activations on update are silent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment