2

I am trying to up date a form on a website that makes a dropdown list of all the sites available for registration on a WP Network site. It orders it by ID but I need to change that to Alphabetical. What it also does is there is a bit of code that allows some of the sites to not show in the list if that are marked as excluded. I also see that it is using code that is deprciated since wp 4.6 so it needs to be updated in general.

The issue is that I see that I don't know how to do this and the original coder is long gone and my knowledge of this is limited.

Can anyone help me update this code to be up to current standards and sort by alphabetical?

            <select id="blog2register" name="blog2register">
        <option disabled="disabled" selected="selected" value="0000"> <?php _e(' - - - Choose a condo group to register for - - - ','msregister'); ?></option>
        <?php 
        $args = array( 'limit'      => 10000, );
        $TheBlogs = wp_get_sites($args);
        foreach($TheBlogs as $blog){
            $theBlog = get_blog_details( $blog['blogname'], true );

            if(get_site_option('msregister_blog1_id')!=$blog['blogname'] && get_site_option('msregister_exclude_'.$blog['blogname'])!='yes'){ 
            echo '<option class="" value="'.$blog['blogname'].'">'.$theBlog->blogname.'</option>';
            }

        }
         ?>
        </select>

1 Answer 1

2

So the main things that have changed that need updating due to the deprecation of wp_get_sites() is to change that to get_sites(). When using get_sites(), $TheBlogs will be an array of WP_Site objects, rather than an array of arrays. This means that to get the details of the site, rather than using get_blog_details() you can use $blog->__get( 'blogname' ).

The other part, sorting alphabetically, will require sorting the resulting array, as get_sites() doesn't appear to have an argument for ordering by blogname.

// Get blogs with get_sites(), which uses 'number' instead of 'limit'. 
$blogs = get_sites( ['number' => 1000] );

// Sort blogs alphabetically.
uasort( 
    $blogs, 
    function( $a, $b ) {
        // Compare site blog names alphabetically for sorting purposes.
        return strcmp( $a->__get( 'blogname' ), $b->__get( 'blogname' ) );
    }
);

foreach ( $blogs as $blog ) {
    // Store blog name in variable for later use.
    $blogname = $blog->__get( 'blogname' );

    // Check blog is not excluded.
    if ( 
        get_site_option( 'msregister_blog1_id' ) != $blogname && 
        get_site_option( 'msregister_exclude_' . $blogname ) != 'yes'
    ) { 
        // Output option tag, escaping the blog name as appropriate.
        printf(
            '<option value="%s">%s</option>',
            esc_attr( $blogname ),
            esc_html( $blogname )
        );
    }
}
1
  • Thank you for this. Unfortunately it doesn't seem to be working. The order hasn't changed and now it's showing the items that are supposed to be marked as excluded :(
    – Tamerax
    Commented Mar 19, 2018 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.