Skip to content

Instantly share code, notes, and snippets.

@Micemade
Created March 2, 2019 21:21
Show Gist options
  • Save Micemade/d83748178137c46cfcea20f16320592f to your computer and use it in GitHub Desktop.
Save Micemade/d83748178137c46cfcea20f16320592f to your computer and use it in GitHub Desktop.
Change plugin load order (prioritize it) by shifting it to first place in array of active plugins.
<?php
/**
* Plugin (WooCommerce) loads first
*
* @return void
* make sure plugin (WooCommerce) is loaded before any other,
* this was created because of hook priority issues.
*/
function prefix_load_plugin_first() {
// Get all active plugins from options.
$plugins = get_option( 'active_plugins', array() );
if ( ! empty( $plugins ) ) {
foreach ( $plugins as $key => $plugin ) {
// Check if WooCommerce is already loaded.
if ( 'woocommerce/woocommerce.php' === $plugin ) {
// If WooCommerce is already first to load, abort.
if( 0 === $key ) {
return;
}
// "Cache" WC from the list.
$my_plugin_first = $plugin;
// Remove it from active plugins array.
unset( $plugins[ $key ] );
}
}
// From "cache", put WC to first place in array.
array_unshift( $plugins, $my_plugin_first );
// Update options.
update_option( 'active_plugins', $plugins );
}
}
add_action( 'admin_init', 'prefix_load_plugin_first' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment