Skip to content

Instantly share code, notes, and snippets.

@panoslyrakis
Created May 18, 2017 18:43
Show Gist options
  • Select an option

  • Save panoslyrakis/734bd85927d82f1035bb2487dc494f05 to your computer and use it in GitHub Desktop.

Select an option

Save panoslyrakis/734bd85927d82f1035bb2487dc494f05 to your computer and use it in GitHub Desktop.
Sort Blog Categories by name
<?php
/*
Plugin Name: Blog Categories Sorting
Plugin URI: https://premium.wpmudev.org/
Description: Sort Blog Categories by name
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
if( ! class_exists( 'WPMUDEV_BlogCategories_Sort' ) ){
class WPMUDEV_BlogCategories_Sort{
private static $_instance = null;
public $order;
public $order_by;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_BlogCategories_Sort();
}
return self::$_instance;
}
private function __construct(){
//For desc change to SORT_ASC
$this->order = SORT_ASC;
$this->order_by = 'name';
add_filter( 'nbt_signup_templates', array( $this, 'resort_blog_categories' ), 10, 1 );
}
public function resort_blog_categories( $templates ){
return $this->array_msort( $templates, array( $this->order_by => $this->order ) );
}
function array_msort( $array, $cols ){
$colarr = array();
foreach ( $cols as $col => $order) {
$colarr[$col] = array();
foreach ( $array as $k => $row ) { $colarr[$col]['_'.$k] = strtolower( $row[$col]) ; }
}
$eval = 'array_multisort(';
foreach ( $cols as $col => $order ) {
$eval .= '$colarr[\''.$col.'\'],'.$order.',';
}
$eval = substr( $eval,0,-1 ).');';
eval( $eval );
$ret = array();
foreach ( $colarr as $col => $arr ) {
foreach ( $arr as $k => $v ) {
$k = substr( $k,1 );
if (!isset($ret[$k])) $ret[$k] = $array[$k];
$ret[$k][$col] = $array[$k][$col];
}
}
return $ret;
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['WPMUDEV_BlogCategories_Sort'] = WPMUDEV_BlogCategories_Sort::get_instance();
}, 10 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment