How to add custom column : IP, link,

Hello,

Would you know how I can add different columns to the overview of my sites, in particular :

  • The IP address of the server
  • Custom link to the server admin
  • Number of articles on each site
  • Custom info to be added manually (example: Referrings Domain, TF, CF,…)

Thanks in advance to the team

Hi Cyril,

To add custom columns to the Manage Sites table, you can use the following hooks:

// Create column and sate column header
add_filter( 'mainwp_sitestable_getcolumns', 'mycustom_mainwp_sitestable_getcolumns', 10, 1 );
function mycustom_mainwp_sitestable_getcolumns( $columns ) {
	$columns['ipaddress'] = "IP Address";
	return $columns;
}
// Set column data
add_filter( 'mainwp_sitestable_item', 'mycustom_mainwp_sitestable_item', 10, 1 );
function mycustom_mainwp_sitestable_item( $item ) {
    $website = \MainWP\Dashboard\MainWP_DB::instance()->get_website_by_id( $item['id'], true );
	$website_info = \MainWP\Dashboard\MainWP_DB::instance()->get_website_option( $website, 'site_info' );
	$website_info = !empty( $website_info ) ? json_decode( $website_info, true ) : array();
	$ipaddress = $website_info['ip'];
	$item['ipaddress'] = $ipaddress;
    return $item;
}

In this example, you can see how to add the IP column and it’s simple because the IP info is already stored in the Dashboard DB.

However to create columns that will show custom info that is not stored in the MainWP Dashboard database, such as server admin URL, Referring Domains…), first, you need to create custom site fields on the Site Edit page and enter the values, after that, you can use those hooks to show the data in the table.

Here you can see how to add custom fields to the Edit form:
https://managers.mainwp.com/t/how-to-add-custom-field-to-child-site-edit-screen/6785/7

4 Likes

Hello Bogdan,

Is it also possible to sort the Manage Sites table on custom column “IP Address” or search for an “IP Address”?

Best regards,

Vincent

Hi Vincent,

By default, the new column will be included in the search feature.
However, it’s not sortable by default.
I will need to check with the dev team if this can be tweaked in the next update.

2 Likes

Hello Bogdan,

Thanks. We have added a custom field via Extensions → Pro Reports → Tokens. This custom field is not included in search feature, it is? And how can we do that?

Best regards,

Vincent

Hi @webdesignIW,

Regarding the sorting ability of custom columns, there is another filter that can be used to add the column in the array of sortable columns, for example the IP Address column that we created in the previous example will be sortable if you use this snippet:

add_filter( 'mainwp_sitestable_sortable_columns', 'mycustom_mainwp_sitestable_sortable_columns' , 10, 2 );
function mycustom_mainwp_sitestable_sortable_columns( $sortable_columns ) {
        $sortable_columns['ipaddress'] = array( 'ipaddress', false );
        return $sortable_columns;
}

Regarding the search ability, the Search feature of the Manage Site table, but default, includes all data that is in the table. Maybe I am missing something in your setup. Can you please provide me with more details?

Hello Bogdan,

I’ve added your code to be able to sort on the custom column. After that the sort icons (ascending / descending) appear but when I try to sort on the custom column the table is not sorted. We’re using MainWP version 4.4.1 and WP 6.2.

Searching for data used in the custom field is not working. We added this custom field via extensions → Pro Reports → Tokens and added some data in the custom field for some clients. When we search on the data we get the message “No websites found” and a few seconds after this search all websites are shown again.

Please let me know how to solve this.

Best regards,

Vincent

I’ve tested this code as well as I’m showing the IP in my table too. It does show the sort up/down icons, but it doesn’t sort anything.
I also tried to find the filter in the docs on mainwp.dev, but couldn’t find it there.

Hi @josklever,

I assume you have the Optimize for Big Networks and Shared Hosting option enabled on your setup.
I reviewed the code and found that the sortable column filter is not applied in this mode.

If the option is disabled, sorting should work fine.

However, I will review this further with our dev team and have them update the “Optimized” mode too.

1 Like

Yes, that option is indeed enabled. If sorting would need too much resources, it might also be an option to filter the sites based on an IP address (partial, starting with…).

Hello Bogdan,

I can confirm changing value for “Optimize for shared hosting or big networks” to “Off” makes it possible to sort the custom fields.

Best regards,

Vincent

1 Like

Hello Bogdan,

I can confirm changing value for “Optimize for shared hosting or big networks” to “Off” makes it possible to sort the custom fields. But the loading of the sites table is very slow because of this. How can we improve the loading time?

Best regards,

Vincent

The loading time is dependent on the performance of the server.
Please take a look at our help document for minimum and recommended system requirements, including some general guidelines for Dashboards with higher child site count:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.