Skip to content

Instantly share code, notes, and snippets.

@curtisnehring
Last active April 3, 2021 22:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curtisnehring/0241c35ee77744137d8adf17c099c480 to your computer and use it in GitHub Desktop.
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
<?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