5

I'm trying to get info into my network sites page in a column. Below works great to grab the network site ID for each site.

How can I make it display the option_value for the item (blog_expire) in the option_name column within my blogname_options table?

class Add_Blog_ID {
public static function init() {
    $class = __CLASS__ ;
    if ( empty( $GLOBALS[ $class ] ) )
        $GLOBALS[ $class ] = new $class;
}
public function __construct() {
    add_filter( 'wpmu_blogs_columns', array( $this, 'get_id' ) );
    add_action( 'manage_sites_custom_column', array( $this, 'add_columns' ), 10, 2 );
    add_action( 'manage_blogs_custom_column', array( $this, 'add_columns' ), 10, 2 );
    add_action( 'admin_footer', array( $this, 'add_style' ) );
}
public function add_columns( $column_name, $blog_id ) {
    if ( 'blog_id' === $column_name )
        echo $blog_id;
    return $column_name;
}
// Add in a column header
public function get_id( $columns ) {
    $columns['blog_id'] = __('ID');
    return $columns;
}
public function add_style() {
    echo '<style>#blog_id { width:7%; }</style>';
}
}
add_action( 'init', array( 'Add_Blog_ID', 'init' ) );

2 Answers 2

5

here is a modified version of your class that should work:

class Add_Blog_ID {
    public static function init() {
        $class = __CLASS__ ;
        if ( empty( $GLOBALS[ $class ] ) )
            $GLOBALS[ $class ] = new $class;
    }
    public function __construct() {
        add_filter( 'wpmu_blogs_columns', array( $this, 'get_id' ) );
        add_action( 'manage_sites_custom_column', array( $this, 'add_columns' ), 10, 2 );
        add_action( 'manage_blogs_custom_column', array( $this, 'add_columns' ), 10, 2 );
        add_action( 'admin_footer', array( $this, 'add_style' ) );
    }
    public function add_columns( $column_name, $blog_id ) {
        if ( 'blog_id' === $column_name ){
            echo $blog_id;
            //render column value
        }elseif( 'blog_expire' === $column_name ){
            echo get_blog_option($blog_id,'blog_expire',"Default Value To Show if none");
        }
        return $column_name;
    }
    // Add in a column header
    public function get_id( $columns ) {
        $columns['blog_id'] = __('ID');
        //add extra header to table
        $columns['blog_expire'] = __('Blog Expires');

        return $columns;
    }
    public function add_style() {
        echo '<style>#blog_id { width:7%; }</style>';
    }
}
add_action( 'init', array( 'Add_Blog_ID', 'init' ) );
0
2

This is not an answer to the question, but an extra feature to JonnyPlow & Bainternet code.

I wanted the ID column at the beginning, not the end. This can be achieved this way:

public function get_id( $columns ) {
    $columns['blog_expire'] = __('Blog Expires');
    $in = array('blog_id' => 'ID');
    $columns = $in + $columns; // array_unshift don't work here, don't ask me why
    return $columns;
}

But, if we want to fine tune and put it as the second (or 3rd) column, then an extra function comes handy.

  • although the admin_footer is harmless in other screens, I put the correct suffix for only loading in sites.php
  • as blog_expire was not rendering anything for me, I changed to template for a quick view of what theme each site is using.
class Add_Blog_ID {
    public static function init() {
        $class = __CLASS__ ;
        if ( empty( $GLOBALS[ $class ] ) )
            $GLOBALS[ $class ] = new $class;
    }
    public function __construct() {
        add_filter( 'wpmu_blogs_columns', array( $this, 'get_id' ) );
        add_action( 'manage_sites_custom_column', array( $this, 'add_columns' ), 10, 2 );
        add_action( 'manage_blogs_custom_column', array( $this, 'add_columns' ), 10, 2 );
        add_action( 'admin_footer-sites.php', array( $this, 'add_style' ) );
    }

    private function array_put_to_position(&$array, $object, $position, $name = null) {
        $count = 0;
        $return = array();
        foreach ($array as $k => $v) {  
                // insert new object
                if ($count == $position) {  
                        if (!$name) $name = $count;
                        $return[$name] = $object;
                        $inserted = true;
                }  
                // insert old object
                $return[$k] = $v;
                $count++;
        }  
        if (!$name) $name = $count;
        if (!$inserted) $return[$name];
        $array = $return;
        return $array;
    }

    public function add_columns( $column_name, $blog_id ) {
        if ( 'blog_id' === $column_name ) {
            echo $blog_id;
            //render column value
        } elseif ( 'template' === $column_name ) {
            echo get_blog_option($blog_id, 'template', "Default Value To Show if none");
        }
        return $column_name;
    }
    // Add in a column header
    public function get_id( $columns ) {
        $columns = $this->array_put_to_position($columns, 'ID', 1, 'blog_id');
        $columns['template'] = __('Using Theme');
        return $columns;
    }

    public function add_style() {
        echo '<style>#blog_id { width:7%; }</style>';
    }
}
add_action( 'init', array( 'Add_Blog_ID', 'init' ) );
3
  • 1
    This is excellent! I needed the Theme for a site also to show. I had been trying current_theme. I started searching for documentation on all the available options.
    – kaplan
    Aug 18, 2022 at 21:10
  • Wow! This is some ancient code!! Glad it was useful :)
    – brasofilo
    Aug 21, 2022 at 2:07
  • I have tried similar code to add a column to the Sites list page by putting code in a plugin with the appropriate filter. But plugins are not loaded on the Network, Sites page, so my filter never gets executed. See wordpress.stackexchange.com/q/413584/29416 and the note at the end of the question. Have not figured out a solution yet after much searching... Feb 8, 2023 at 20:01

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.