List all WordPress Network sites using simple shortcode [updated]

This is a solution written basing on Tom J Nowell‘s idea and answer to this question. It prints out list of all sites in WordPress Multisite installation (alphabetically sorted) as simple line (separated with pipe — |).

Single site or single theme approach

To get this solution running, you need to edit shortcodes.php file from your currently selected theme. To do this, you need to:

  • visit Themes > Editor section from left sidebar,
  • select theme from Select theme to edit list and click Select,
  • click shortcodes.php file in the right sidebar,
  • in code editor, near the end of this file (before first occurrence of add_shortcode call) add code given below,
  • save changes by clicking Update File.

In multi-site installation Themes > Editor section is available only in network administration panel (i.e. My Sites > Network Admin > Themes in top menu). I don’t know, where to find corresponding section in single-site WordPress installation.

It may happen, that your particular theme does not contain shortcodes.php file. In this case or in any other case, when you can’t access shortcodes.php file in themes editor (or themes editor itself) the only option, you’re left with, is to:

  • access your WordPress installation via FTP,
  • navigate to themes folder and your theme subfolder,
  • copy shortcodes.php file from remote FTP server to your local computer (or create it locally, if it does not exist),
  • add following code to the end of your locally stored file,
  • save shortcodes.php file and upload it back to your FTP server, overwriting existing one.

Code to be added to shortcodes.php file:

[code language=”php”]
function theme_list_all_network_sites()
{
global $wpdb;

$result = ”;
$sites = array();
$blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE spam = ‘0’ AND deleted = ‘0’ and archived = ‘0’ and public=’1’"));

if(!empty($blogs))
{
foreach($blogs as $blog)
{
$details = get_blog_details($blog->blog_id);

if($details != false)
{
$url = $details->siteurl;
$name = $details->blogname;

if(!(($blog->blog_id == 1) && ($show_main != 1)))
{
$sites[$name] = $url;
}
}
}

ksort($sites);

$count = count($sites);
$current = 1;

foreach($sites as $name=>$url)
{
$result.= ‘<a href="’.$url.’">’.$name.'</a>’;
$result.= ($current == $count) ? "n" : ‘ | ‘;

++$current;
}
}

return $result;
}
[/code]

Then scroll down to the end of file and after last occurence of add_shortcode add your own call:

[code language=”php”]
add_shortcode(‘network_list’, ‘theme_list_all_network_sites’);
[/code]

Click Update File to save your changes.

Now, whenever anyone use [network_list] shortcode to in post, page or theme element, a list of network sites will be printed in place of that shortcode.

Of course, you can use this approach to introduce any shortcode to your site and bind any PHP code to it, that will result in doing anything, not just listing your sites.

Network-wide approach

This WordPress Forum has answer on how to create a shortcode that is available across entire blog network, on all pages, posts etc.

Basically, you have to put your PHP code, that defines your shortcode, to some file placed in /wp-content/mu-plugins folder. If this folder does not exists in your WordPress installation, simply create it. Read more in this StackOverflow answer or in WordPress Codex.

2 comments on “List all WordPress Network sites using simple shortcode [updated]

  1. Rob

    Hi, how i can add this code in my wordpress theme? i didn’t find the file “shortcode.php”
    thank you in advance
    best regards
    Rob

    1. admin

      For single site or single theme approach it was written badly and I have updated this entire section with more details. See, if it helps you now.

      For multi-site installation, follow to mentioned article, as right now (three and a half year since writing original text) I don’t remember the solution. Sorry.

Leave a Reply