All blogs in one place, by alphabetical order

Hi guys,

Got some issues with php code in here.
I was searching for simple php code which will pull out from my database all new users which are registered in my multisite. So i found something but it's not quite exactly what i was hoping for.

On the web site, stackexchange i found out little php script which does exactly what i was hoping for, just pulling all users from database with shordcode insite where can i paste it in post or page to see all registered users ever. There are two problems:

1. I have no idea how to remove one specific line of the code which is not in use anymore
this line is:
($bcount = get_blog_count();)
2. Is there a way to show this blogs in page/post by alphabetical order?

Here is the code im using:

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;
}

add_shortcode('network_list', 'theme_list_all_network_sites');

and i have to add. Since this code has to be inserted in themes shortcode.php file, which i couldn't find, i've used general shordcode file which i found in wp-includes folder.

  • Predrag Dubajic

    Hi @Eldin,

    Hope you're doing well today :slight_smile:

    1. I have no idea how to remove one specific line of the code which is not in use anymore
    this line is:

    I'm not sure I understand this part, where is that code located, I don't see it?

    2. Is there a way to show this blogs in page/post by alphabetical order?

    In the code you are using replace this line of code:
    ksort($sites);
    With this:
    asort($sites);
    Save the changes and that should be it.

    Since this code has to be inserted in themes shortcode.php file, which i couldn't find, i've used general shordcode file which i found in wp-includes folder.

    You should never make changes to core WordPress files, if your theme doesn't have shortcodes.php file you can place it inside functions.php which every theme should have.
    There is also an option to create mu-plugin (must use plugin), go to your wp-content/ folder and create new folder inside named mu-plugins if there isn't one already. Now in that folder create new .php file with any name and add your code inside just make sure that before the code you have this line
    <?php

    Let me know if you need more help with this :slight_smile:

    Best regards,
    Predrag

  • Voxxyz

    Hi @Predrag and thanks for quick reply,

    I've messed up, just realized. Gonna explain further right now.

    Im already using mu-plugins and already having function.php inside that folder with this few lines of code for avatars:

    <?php
    // always show admin bar
    function pjw_login_adminbar( $wp_admin_bar) {
    	if ( !is_user_logged_in() )
    	$wp_admin_bar->add_menu( array( 'title' => __( 'Log In' ), 'href' => wp_login_url() ) );
    }
    add_action( 'admin_bar_menu', 'pjw_login_adminbar' );
    add_filter( 'show_admin_bar', '__return_true' , 1000 );

    and this works just fine.

    Now im adding this updated line of code im using in explination above, this code, with replacement you help me with:

    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;
    }

    now the simple line of shorcode i needed to add more below this last bracket was this:

    add_shortcode('network_list', 'theme_list_all_network_sites');

    AND finally the whole new function.php in mu-plugins looks like this and doesn't work. Error in line 375, (this last bracket):

    <?php
    // always show admin bar
    function pjw_login_adminbar( $wp_admin_bar) {
    	if ( !is_user_logged_in() )
    	$wp_admin_bar->add_menu( array( 'title' => __( 'Log In' ), 'href' => wp_login_url() ) );
    }
    add_action( 'admin_bar_menu', 'pjw_login_adminbar' );
    add_filter( 'show_admin_bar', '__return_true' , 1000 );
    
    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;
    }
    add_shortcode('network_list', 'theme_list_all_network_sites');

    It's a lot of code and i hope you understand me now...

  • Predrag Dubajic

    Hey @Eldin,

    This is the code I tried adding to my mu-plugin and didn't see the warning:

    <?php
    // always show admin bar
    function pjw_login_adminbar( $wp_admin_bar) {
        if ( !is_user_logged_in() )
        $wp_admin_bar->add_menu( array( 'title' => __( 'Log In' ), 'href' => wp_login_url() ) );
    }
    add_action( 'admin_bar_menu', 'pjw_login_adminbar' );
    add_filter( 'show_admin_bar', '__return_true' , 1000 );
    
    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;
                    }
                }
            }
    
            asort($sites);
    
            $count = count($sites);
            $current = 1;
    
            foreach($sites as $name=>$url)
            {
                $result.= '<a href="'.$url.'">'.$name.'</a>';
                $result.= ($current == $count) ? "\n" : ' | ';
    
                ++$current;
            }
        }
    
        return $result;
    }
    add_shortcode('network_list', 'theme_list_all_network_sites');

    May I suggest different solution for showing all your sub sites?
    Try installing the Blogs Directory and after few simple settings you should be able to create a page where all your sub sites will be shown, including the search function.

    Let me know if this helps.

    Best regards,
    Predrag

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.