1

I created some blogs in a multisite network but now i need the possibility to control the order of those blogs.

There is a list of all blogs on the main page and it is ordered by blog id by default, i guess.

My idea is to add some kind of extra option field for every blog (maybe directly under "wp-admin/network/site-info.php?id=##")and sort the list by this number. Is there a better solution?

  • please define "better". In principal there are several ways to do it an without limiting the scope of your question it is just too broad. – Mark Kaplun Oct 8 '14 at 16:38
  • With "better" i mean a less complex solution, maybe a plugin, a special hook or another idea. At the moment i try to implement my idea with help of this post but i have problems with saving the value. – Paul Geisler Oct 9 '14 at 7:51
0

I ended up implementing my idea.

I've described the creation of the custom input field on the Site Info Screen unter Network in this Post. Here is the complete code again:

<?php
// Create custom Input Field on the WP-Admin > Network > Site Info Screen
// for the possibility to order sites manually 
add_action('admin_footer', 'pg_custom_site_options');
function pg_custom_site_options(){

    global $pagenow;

    if( 'site-info.php' == $pagenow ) {

        global $details;

        $blog_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
        if ( ! $blog_id ) wp_die( __('Invalid site ID.') );

        $saved_value = get_blog_option( $blog_id, 'blog_order');

        ?>
        <table style="display: none;">
            <tr id="user16975_custom_options">
                <th scope="row">Order</th>
                <td><input type="text" size="5" name="blog[blog_order]" value="<?php echo esc_attr( $saved_value ) ?>" ></td>
            </tr>
        </table>
        <script>
            jQuery(function($){
                $('.form-table tbody').append($('#user16975_custom_options'));
            });
        </script>
        <?php

    }
}

add_action('admin_init', 'pg_save_custom_site_options');
function pg_save_custom_site_options(){

    global $pagenow;

    if( 'site-info.php' == $pagenow &&
        isset($_REQUEST['action']) &&
        'update-site' == $_REQUEST['action']
    ) {
        // Use a default value here if the field was not submitted.
        $new_field_value = '0';

        if ( isset( $_POST['blog']['blog_order'] ) ) {
            $new_field_value = intval( $_POST['blog']['blog_order'] );

            // save option into the database
            if( is_int($new_field_value) ){
                update_blog_option( $_POST['id'], 'blog_order', $new_field_value );
            }

        }

    }
}
?>

After every site got its order number i've placed it in the wp_get_sites-array

$all_standorte =  wp_get_sites( $args );

// Get the order nr of each site and save it in the array
$pos = 0;
foreach( $all_standorte as $standort){

    $standort_id = $standort['blog_id'];

    $blog_order = get_blog_option( $standort_id, 'blog_order', '0');

    $all_standorte[$pos]['blog_order'] = $blog_order;

    $pos++;
}

The final step is sorting the array with usort().

// Sort Sites by Custom Order
function sort_sites_by_order($a, $b){

    // ascending (aufsteigend)
    return $a['blog_order'] - $b['blog_order'];

    // descending (absteigend, der juengste Artikel zuerst)
    // return $b['blog_order'] - $a['blog_order'];
}
usort($all_standorte, 'sort_sites_by_order');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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