Skip to content

Instantly share code, notes, and snippets.

@youri--
Last active August 29, 2015 14:06
Show Gist options
  • Save youri--/29aa1ad44db1bfe7000a to your computer and use it in GitHub Desktop.
Save youri--/29aa1ad44db1bfe7000a to your computer and use it in GitHub Desktop.
Reorder My Sites, fixed notices and now sorting my-sites page as well
<?php
/*
Plugin Name: Reorder My Sites
Plugin URI: http://judenware.com/projects/wordpress/reorder-mysites/
Description: Reorders the My Sites dropdown menu in the Admin Bar alphabetically. It keeps the main blog at the top.
Author: ericjuden
Version: 1.1.002
Author URI: http://www.judenware.com
Network: true
*/
class Reorder_My_Sites {
function init(){
if( is_multisite() ){
add_action( 'admin_bar_menu', array(__CLASS__, 'admin_bar_menu') );
if ( is_admin() ) {
add_filter( 'get_blogs_of_user', array( __CLASS__, 'get_blogs_of_user' ), 10, 3 );
}
}
}
public static function sort_blogs($blogs) {
if ( empty( $blogs ) || ! empty( $blogs ) && ( $firstblog = reset($blogs) ) && ! isset( $firstblog->blogname )) {
return $blogs;
}
$sorted_blogs = $blog_names = array();
foreach ($blogs as $blog_id => $blog){
$blog_names[$blog_id] = strtoupper($blog->blogname);
}
// Order by name
asort($blog_names);
// Get main blog ID (first in blog list)
reset($blogs);
$main_blog_id = key($blogs);
// Remove main blog from list...we want that to only show at the top
unset($blog_names[ $main_blog_id ]);
// Add main blog first in list
$sorted_blogs[ $main_blog_id ] = $blogs[ $main_blog_id ];
// Add others back in alphabetically
foreach($blog_names as $blog_id => $name){
$sorted_blogs[ $blog_id ] = $blogs[ $blog_id ];
}
return $sorted_blogs;
}
public static function get_blogs_of_user($blogs, $user_id, $all) {
$sorted_blogs = self::sort_blogs($blogs);
return $sorted_blogs;
}
public static function admin_bar_menu() {
global $wp_admin_bar;
$blogs = $wp_admin_bar->user->blogs;
$sorted_blogs = self::sort_blogs($blogs);
$wp_admin_bar->user->blogs = $sorted_blogs;
}
}
Reorder_My_Sites::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment