Add new column to sites page

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' ) );

Related posts

Leave a Reply

2 comments

  1. 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' ) );
    
  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' ) );