Skip to content

Instantly share code, notes, and snippets.

@webaware
Last active May 14, 2023 03:56
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 webaware/b914967f0ce8110c775cd12ddc546283 to your computer and use it in GitHub Desktop.
Save webaware/b914967f0ce8110c775cd12ddc546283 to your computer and use it in GitHub Desktop.
Simple dashboard widget for listing WordPress multisite sites, and accompanying shortcode for the front end.
<?php
/*
Plugin Name: Network Lister
Plugin URI: https://gist.github.com/webaware/b914967f0ce8110c775cd12ddc546283
Update URI: network-lister
Description: simple dashboard widget for listing network sites
Version: 0.0.5
Author: WebAware
Author URI: https://shop.webaware.com.au/
*/
/*
copyright (c) 2018-2023 WebAware Pty Ltd
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
namespace webaware\network_lister;
if (!defined('ABSPATH')) {
exit;
}
/**
* get a list of all sites on the network
*/
function get_list(bool $for_admin) : string {
$output = '';
$current_site_id = get_current_blog_id();
$sites = get_sites();
if (is_array($sites)) {
$list = [];
$template = $for_admin ? '<li><a href="%s/wp-admin/"%s>%s — %s</a></li>' : '<li><a href="%s/"%s>%s — %s</a></li>';
foreach ($sites as $site) {
$details = get_blog_details($site->blog_id);
$style = $current_site_id == $site->blog_id ? ' style="font-weight:bold"' : '';
$list[$details->blogname] = sprintf($template,
esc_url(set_url_scheme($details->siteurl)),
$style,
esc_html($details->blogname),
esc_html($details->blog_id));
}
uksort($list, 'strcasecmp');
$output = '<ul>' . implode('', $list) . '</ul>';
}
return $output;
}
/**
* show list widget on admin dashboard
*/
function show_lister_widget() : void {
echo get_list(true);
}
/**
* create the new dashboard widget
*/
function add_lister_widget() : void {
wp_add_dashboard_widget('network-lister', 'Network List', __NAMESPACE__ . '\\show_lister_widget');
}
add_action('wp_dashboard_setup', __NAMESPACE__ . '\\add_lister_widget');
add_action('wp_network_dashboard_setup', __NAMESPACE__ . '\\add_lister_widget');
/**
* add shortcode for network list
*/
add_shortcode('network_lister', function() : string {
return get_list(false);
});
/**
* show the current site ID on every page footer
*/
add_filter('admin_footer_text', function(string $footer_text) : string {
$footer_text .= ' &bull; blog_id = ' . get_current_blog_id();
return $footer_text;
}, 20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment