Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active February 23, 2024 00:16
Show Gist options
  • Save wpmudev-sls/de71d5c68064221b6abdc994906ff7c1 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/de71d5c68064221b6abdc994906ff7c1 to your computer and use it in GitHub Desktop.
Add shortcode list all blog sites
<?php
/**
* Plugin Name: Add shortcode list all blog sites
* Description: Add shortcode list all blog sites ([wpmudev_list_all_blogs]) - 1155576029146873
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
return;
}
add_action( 'after_setup_theme', 'wpmudev_add_shortcode_list_all_sub_sites_func' );
function wpmudev_add_shortcode_list_all_sub_sites_func(){
if( ! is_multisite() ) return;
add_action( 'wpmueditblogaction', 'wpmudev_add_thumbnail_option_for_sub_site', 9999 );
function wpmudev_add_thumbnail_option_for_sub_site(){
$option_name = 'wpmudev_featured_image_subsite';
$option_value = get_option( $option_name );
?>
<tr class="form-field">
<th scope="row"><label for="<?php echo esc_attr( $option_name ); ?>"><?php _e('Feature Image Link','wpmudev') ?></label></th>
<td><input class="<?php echo $class; ?>" name="option[<?php echo esc_attr( $option_name ); ?>]" type="text" id="<?php echo esc_attr( $option_name ); ?>" value="<?php echo esc_attr( $option_value ); ?>" size="40" /></td>
</tr>
<?php
}
add_shortcode( 'wpmudev_list_all_blogs', 'wpmudev_list_all_blogs' );
function wpmudev_list_all_blogs( $atts, $content='' ){
$atts = shortcode_atts( array(
'site__in' => '',// separated by comma (,)
'site__not_in' => '',
'orderby' => '',
'order' => '',// ASC | DESC
'show_thumbnail' => 1,
'show_blogname' => 1,
'show_blogdescription' => 1,
'open_new_tab' => 1,
'use_site_icon' => 1,
'mtl_if_no_icon' => 1,
'default_icon' => ''//default icon link
), $atts );
$query_args = array();
if( ! empty( $atts['site__in'] ) ){
$query_args['site__in'] = explode(',', $atts['site__in']);
}
if( ! empty( $atts['site__not_in'] ) ){
$query_args['site__not_in'] = explode(',', $atts['site__not_in']);
}
if( $atts['orderby'] !== '' ){
if( strpos( $atts['orderby'], ',') ){
$atts['orderby'] = explode(',', $atts['orderby']);
}
$query_args['orderby'] = $atts['orderby'];
}
if( ! empty( $atts['order'] ) ){
$query_args['order'] = $atts['order'];
}
$query_args = wp_parse_args( $query_args, array(
'limit' => 0,
'public' => true,
'spam' => false,
'deleted' => false,
// only plubic
'archived' => false,
'mature' => false,
// 'fields' => 'ids'
) );
// get sites only visible from 4.6.0
if ( function_exists( 'get_sites' ) ) {
$sites = get_sites( $query_args );
} else {
$sites = wp_get_sites( $query_args );
}
if( $sites ){
$target = ! empty( $atts['open_new_tab'] ) ? '_blank' : '_self';
$content ='<div id="wpmudev-list-blogs">';
$moved_to_last = [];
foreach( $sites as $site ){
$move_to_last = 0;
switch_to_blog( $site->blog_id );
$new_blog ='<div class="site-info site-info-'. $site->blog_id .'">';
$site_name = esc_html( get_bloginfo( 'name', 'display' ) );
$before_link = sprintf('<a href="%s" title="%s" target="%s">', home_url(), $site_name, $target );
$after_link = '</a>';
if( $atts['show_thumbnail'] ){
$thumbnail = get_option( 'wpmudev_featured_image_subsite' );
if( empty( $thumbnail ) ){
if( ! empty( $atts['use_site_icon'] ) ){
$thumbnail = get_site_icon_url(512);
}else{
$custom_logo = get_theme_mod( 'custom_logo' );
if( $custom_logo ){
$custom_logo = wp_get_attachment_image_src( $custom_logo, 'full' );
if( $custom_logo ){
$thumbnail = $custom_logo[0];
}
}
}
}
if( empty( $thumbnail ) ){
$move_to_last = 1;
if( ! empty( $default_icon ) ){
$thumbnail = esc_url( $default_icon );
}
}
if( $thumbnail ){
$new_blog .= $before_link;
$new_blog .= '<img class="site-image" alt="'. $site_name .'" src="'. esc_url( $thumbnail ) .'">';
$new_blog .= $after_link;
}
}
if( $atts['show_blogname'] ){
$new_blog .='<h3>';
$new_blog .= $before_link;
$new_blog .= $site_name;
$new_blog .= $after_link;
$new_blog .= '</h3>';
}
if( $atts['show_blogdescription'] ){
$new_blog .='<p>'. esc_html( get_bloginfo( 'description', 'display' ) ) .'</p>';
}
$new_blog .='</div>';
if( $move_to_last ){
$moved_to_last[] = $new_blog;
}else{
$content .= $new_blog;
}
restore_current_blog();
}
if( ! empty( $moved_to_last ) ){
$content .= join('', $moved_to_last);
}
$content .= '</div>';
}
return $content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment