Skip to content

Instantly share code, notes, and snippets.

@DevinWalker
Created December 6, 2015 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DevinWalker/52533f4745ef0eafb308 to your computer and use it in GitHub Desktop.
Save DevinWalker/52533f4745ef0eafb308 to your computer and use it in GitHub Desktop.
/**
* Give function to reorder array keys
* @see: http://php.net/manual/en/function.ksort.php
* reorder the keys of an array in order of specified keynames; all other nodes not in $keynames will come after last $keyname, in normal array order
* @param array &$array - the array to reorder
* @param mixed $keynames - a csv or array of keynames, in the order that keys should be reordered
*/
function give_9812491_my_array_reorder_keys(&$array, $keynames){
if(empty($array) || !is_array($array) || empty($keynames)) return;
if(!is_array($keynames)) $keynames = explode(',',$keynames);
if(!empty($keynames)) $keynames = array_reverse($keynames);
foreach($keynames as $n){
if(array_key_exists($n, $array)){
$newarray = array($n=>$array[$n]); //copy the node before unsetting
unset($array[$n]); //remove the node
$array = $newarray + array_filter($array); //combine copy with filtered array
}
}
}
/**
* Custom Give Gateway Order
*
* @description: Uses a helper function to reorder the gateway. Please customize the function name to limit conflict
*
* @param $gateways
*
* @return mixed
*/
function my_custom_give_gateway_order($gateways){
if(function_exists('give_9812491_my_array_reorder_keys')) {
give_9812491_my_array_reorder_keys($gateways, 'manual,authorize,stripe,offline'); //This is where you place the gateways in the order you need based on the name of the gateway
}
return $gateways;
}
add_filter('give_enabled_payment_gateways', 'my_custom_give_gateway_order', 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment