Directory of Network Sites
Adds a [wpide_sites_directory] shortcode that lists the public sites on the network with their names, taglines and links, cached for an hour.
Opens WPIDE on
Code
if ( ! is_multisite() ) {
return;
}
add_shortcode( 'wpide_sites_directory', function ( $atts ) {
$atts = shortcode_atts( array( 'exclude' => '' ), $atts, 'wpide_sites_directory' );
$exclude = array_filter( array_map( 'absint', explode( ',', $atts['exclude'] ) ) );
$key = 'wpide_sites_dir_' . md5( implode( ',', $exclude ) );
$html = get_site_transient( $key );
if ( false !== $html ) {
return $html;
}
$sites = get_sites( array(
'public' => 1,
'archived' => 0,
'spam' => 0,
'deleted' => 0,
'site__not_in' => $exclude,
'number' => 500,
'update_site_meta_cache' => false,
) );
$html = '<ul class="wpide-sites-directory">';
foreach ( $sites as $site ) {
$tagline = get_blog_option( $site->blog_id, 'blogdescription' );
$html .= sprintf(
'<li><a href="%1$s">%2$s</a>%3$s</li>',
esc_url( $site->home ),
esc_html( $site->blogname ),
$tagline ? ' – <span>' . esc_html( $tagline ) . '</span>' : ''
);
}
$html .= '</ul>';
set_site_transient( $key, $html, HOUR_IN_SECONDS );
return $html;
} );
Notes
Archived, spam, deleted and search-engine-hidden sites are left out. Set exclude="1,4" to hide particular blog IDs.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.