4

I want to add an additional column in the multisite Sites page (sites.php). Found this code on this SO:

add_filter('wpmu_blogs_columns', 'xx_add_expired_date_column');

function xx_add_expired_date_column($site_columns) {
    $site_columns['expired_Date'] = 'Expires';
    return $site_columns;
}

/* Populate site_name with blogs site_name */
add_action('manage_sites_custom_column', 'xx_exipred_date_data', 10, 2);

function xx_exipred_date_data($column_name, $blog_id) {
    $current_blog_details = get_blog_details(array('blog_id' => $blog_id));
    echo ucwords($current_blog_details->blogname);
}

The wpmu_blogs_columns filter should add the additional column on the Sites screen. I put this code in a plugin I am developing. Other functions of that plugin are working properly. The plugin has been network activated.

When I look at the Sites list (Network Admin, Sites, or via the Network dashboard Sites, All Sites), the extra column is not there.

If I put a die() statement after the xx_add_expired_date_column function, that 'die' never happens.

I think it may be a loading sequence error - maybe the plugin is not being loaded on the Sites page? What can be done to enable this additional column?

Added 7 Feb 2023

Further testing: the wpmu_blogs_columns filter will work in the theme's (or Child Theme's) functions.php file, but not in a plugin.

Added 8 Feb 2023

As mentioned in the soon-to-be-accepted answer, the column array is $sites_columns (plural 'sites'). And the $column_name is important. The code in the answer will work.

3
  • Did you try the code in a mu plugin?
    – Fayaz
    Commented Feb 7, 2023 at 21:06
  • @Fayaz I thought of putting it in the mu-plugin folder, but my research indicated that plugins there are not part of the 'update available' infrastructure. I haven't figured out why (yet) the filters work in the theme function file, but not in the plugin. Commented Feb 8, 2023 at 2:35
  • See note added 8 Feb 2023. Plugins are not loaded on the Network Admin pages. How to fix? Commented Feb 8, 2023 at 18:34

1 Answer 1

4

I tested your code, fixing the $colum_name issue and it works ok for me.

add_filter('wpmu_blogs_columns', function ($site_columns) {
    $site_columns['expired_Date'] = 'Expires';
    return $site_columns;
});

add_action('manage_sites_custom_column', function ($column_name, $blog_id) {
    if ( 'expired_Date' === $column_name ){
        $current_blog_details = get_blog_details(array('blog_id' => $blog_id));
        // do_action( 'qm/debug', ['Details:', $current_blog_details ] );
        echo ucwords($current_blog_details->last_updated);
    }
}, 10, 2);

You have to check the $column_name otherwise the echo will print in all custom columns.

Both the filter and the action are pretty specific, they will only run on the page /wp-admin/network/sites.php and nowhere else.
I tried the code above using Snippets plugin and also as a mu-plugin and both worked the same, haven't tried as a plugin (maybe you can wrap everything inside a add_action('plugins_loaded', function(){}); hook).

The qm/debug is for the plugin Query Monitor, a must-have for WP devs :)

2
  • Thanks. I had several errors, including the same one you had in your code: the site column array is $sites_columns (plural 'sites'). And another error that eluded me when I looked at the errors.log file. As you mention, the $column_name is an important value to set to get the data displayed in the column. Commented Feb 8, 2023 at 21:39
  • Nice, glad it works now
    – brasofilo
    Commented Feb 8, 2023 at 21:57

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.