Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
List of all blogs in a WordPress Multisite network
<?php
// This code was used to generate a menu showing all blogs on a WordPress
// Multisite installation.
// projects_menu() was used in the template to show a dropdown menu on all the
// sites.
// The shortcode [bloglist] was used in the homepage of the main blog to link to
// all the other blogs.
// Output a single menu item
function projects_menu_entry($id, $title, $link_self)
{
global $blog_id;
if ($id == $blog_id && !$link_self)
return;
echo '<li>';
if ($id == $blog_id)
echo '<strong>';
$url = get_home_url($id);
if (substr($url, -1) != '/') {
// Note: I added a "/" to the end of the URL because WordPress
// wasn't doing that automatically in v3.0.4. YMMV.
$url .= '/';
}
echo '<a href="' . $url . '">' . $title . '</a>';
if ($id == $blog_id)
echo '</strong>';
echo '</li>';
}
// Output the whole menu
// If $link_self is false, skip the current site -
// used to display the menu on the homepage
function projects_menu($link_self = true)
{
global $wpdb;
echo '<ul>';
projects_menu_entry(1, 'Home', $link_self);
$blogs = $wpdb->get_results("
SELECT blog_id
FROM {$wpdb->blogs}
WHERE site_id = '{$wpdb->siteid}'
AND spam = '0'
AND deleted = '0'
AND archived = '0'
AND blog_id != 1
");
$sites = array();
foreach ($blogs as $blog) {
$sites[$blog->blog_id] = get_blog_option($blog->blog_id, 'blogname');
}
natsort($sites);
foreach ($sites as $blog_id => $blog_title) {
projects_menu_entry($blog_id, $blog_title, $link_self);
}
echo '</ul>';
}
// Adds a [bloglist] shortcode, so I can embed the menu into the static homepage.
// Note: I originally put it directly into the template, but that didn't work
// with WPtouch.
add_shortcode('bloglist', function($atts)
{
projects_menu(false);
});
@fatsandrew7

This comment has been minimized.

Copy link

commented May 5, 2012

hi, this code has really helped with what i'm after. i am also interested in pulling each blog's description and theme's logo to list in the same link. i was able to change blogname to blogdescription successfully, but getting both to display did not work.

any help is appreciated!

andrew

@davejamesmiller

This comment has been minimized.

Copy link
Owner Author

commented May 12, 2012

What did you change to try to get it to work?

@fatsandrew7

This comment has been minimized.

Copy link

commented May 14, 2012

well, i actually found another option as get_blog_option does not pull that information. get_blog_details, however does. neither ever really allowed me the flexibility i needed, so i used page redirects and parent/child pages to create the effect i needed.

thank you even still.

@joshmoto

This comment has been minimized.

Copy link

commented Apr 23, 2013

This is awesome - thank you for sharing. This has come in hand so much with my bootstrap theme. But, how can you re-order the sites listed? I want to order by site ID and not alphabetically. I understand you can move this to the top or bottom... projects_menu_entry(1, 'Home', $link_self); - Im just struggling to order the wpdb results.

@bueltge

This comment has been minimized.

Copy link

commented Jul 24, 2013

Small hint for better source. You can remove the SELECT for get all blogs in the network with $blogs = get_blog_list( 0, 'all' ); It is easier, and a core function, maybe easier to maintenance.

@ericterpstra

This comment has been minimized.

Copy link

commented Jul 30, 2013

Not sure why, but get_blog_list is deprecated. See http://codex.wordpress.org/WPMU_Functions/get_blog_list

@derpixler

This comment has been minimized.

Copy link

commented Aug 12, 2013

Here is the reason - http://lists.automattic.com/pipermail/wp-hackers/2010-August/034486.html

get_blog_list() was deprecated for being suicidal. The queries it generated
were death for any scaled MU implementation. This has been discussed in a
few other instances

but an alternative function is already in labor!
http://core.trac.wordpress.org/ticket/14511 or http://core.trac.wordpress.org/attachment/ticket/14511/wp-get-sites.php

as simple solution you can use this query

SELECT * FROM {$wpdb->blogs}, {$wpdb->registration_log} WHERE site_id = '{$wpdb->siteid}' AND {$wpdb->blogs}.blog_id = {$wpdb->registration_log}.blog_id
@cvc1968

This comment has been minimized.

Copy link

commented Feb 24, 2015

Great work. The only question I have is that when I use the shortcode within a page, the blog list gets rendered at the top of the content block, no matter where I place the shortcode. Not sure why this would be, as it seems to be outputting a simple

    list. Any ideas?

@davejamesmiller

This comment has been minimized.

Copy link
Owner Author

commented Mar 25, 2015

@cvc1968 It currently uses echo when it should build a string and return it. (I never tested it with other content in the page so didn't notice that before.)

@bh4kt1

This comment has been minimized.

Copy link

commented Apr 30, 2015

Thanks for this plugin. I think this will save me a lot of time.

List All Blogs in Text Widget? This was my solution:

/\* It is important that these lines be added in this order. The first line prevents WordPress from turning line breaks into paragraph tags, since this keeps shortcodes from working. The second line is the one that makes the shortcodes work. */ add_filter( 'widget_text', 'shortcode_unautop' ); add_filter( 'widget_text', 'do_shortcode' );
@tomygrenier

This comment has been minimized.

Copy link

commented May 11, 2018

@fatsandrew7: Did you find the solution to add the logo of each subsites ?

@FelipeR0cha

This comment has been minimized.

Copy link

commented Mar 14, 2019

alguém poderia me ajudar ? não faço ideia de como usar o código

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.