Skip to content

Instantly share code, notes, and snippets.

@RavanH
Last active September 20, 2017 23:35
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 RavanH/31b5f378590ed02fa31dcc52b18947d3 to your computer and use it in GitHub Desktop.
Save RavanH/31b5f378590ed02fa31dcc52b18947d3 to your computer and use it in GitHub Desktop.
WordPress Multisite: List of all blogs
<?php
function projects_menu( $atts = array() ) {
global $wpdb, $blog_id;
$a = shortcode_atts( array(
'link_self' => false,
'format' => 'ul', // ol, dl... TODO
'slogan' => false, // TODO
'sort_by' => 'title', // or ID TODO
'order' => 'ASC', // or DESC TODO
'show_posts_number' => false // TODO
), $atts );
// TODO switch to https://developer.wordpress.org/reference/functions/get_sites/
$blogs = $wpdb->get_results("
SELECT blog_id
FROM {$wpdb->blogs}
WHERE site_id = '{$wpdb->siteid}'
AND spam = '0'
AND deleted = '0'
AND archived = '0'
");
$sites = array();
foreach ($blogs as $blog) {
$sites[$blog->blog_id] = get_blog_option($blog->blog_id, 'blogname');
}
natsort($sites);
$return = '<ul>';
foreach ($sites as $id => $title) {
if ($id == $blog_id && !$a['link_self']) continue;
$return .= '<li><a href="' . get_home_url($id) . '">' . $title . '</a></li>';
}
$return .= '</ul>';
return $return;
}
add_shortcode( 'site-list', 'projects_menu' );
@RavanH
Copy link
Author

RavanH commented Sep 20, 2017

fix shortcode echoing (must return); prepare for some shortcode attributes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment