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/8d11437ab5c403d1903277f5da740588 to your computer and use it in GitHub Desktop.
Save curtisnehring/8d11437ab5c403d1903277f5da740588 to your computer and use it in GitHub Desktop.
Code to re-order a Wordpress plug-in's load order to place it first or last depending on need
<?php
/***
* Load one of these functions at the top of you plug-in's main .php file
* Which depends on your needs
* _first() will load it first; _last() will load it last
* Need help? => CodeByCurtis.com
**/
/**
* position a plugin to load first or last depending on push or unshift
**/
function load_PLUGIN_NAME_first() {
$this_plugin = plugin_basename(trim(__FILE__));
$active_plugins = get_option('active_plugins');
$this_plugin_key = array_search($this_plugin, $active_plugins);
// remove plugin from active array
array_splice($active_plugins, $this_plugin_key, 1);
// LOAD FIRST: add the plugin to the beginning of the active array
array_unshift($active_plugins, $this_plugin);
// LOAD LAST: add the plugin to the end of the active array
//array_push($active_plugins, $this_plugin);
// update the active plugins setting/option in the db
update_option('active_plugins', $active_plugins);
}
add_action('activated_plugin', 'load_PLUGIN_NAME_first', 15); // run when a plugin is activated
add_action('upgrader_process_complete', 'load_PLUGIN_NAME_first', 15); // run when a plugin is updated; needed because plugin activations on update are silent
// position a plugin to load first or last depending on push or unshift
function load_PLUGIN_NAME_last() {
$this_plugin = plugin_basename(trim(__FILE__));
$active_plugins = get_option('active_plugins');
$this_plugin_key = array_search($this_plugin, $active_plugins);
// remove plugin from active array
array_splice($active_plugins, $this_plugin_key, 1);
// LOAD FIRST: add the plugin to the beginning of the active array
//array_unshift($active_plugins, $this_plugin);
// LOAD LAST: add the plugin to the end of the active array
array_push($active_plugins, $this_plugin);
// update the active plugins setting/option in the db
update_option('active_plugins', $active_plugins);
}
add_action('activated_plugin', 'load_PLUGIN_NAME_last', 15); // run when a plugin is activated
add_action('upgrader_process_complete', 'load_PLUGIN_NAME_last', 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