How to filter the spreadsheet cells before display

When you open the spreadsheet editor, we get the rows from the database using an AJAX request that returns all the information.

You can use a wp filter to modify the array containing the information before it’s returned by the ajax call. This way you can modify values or change the data displayed in the cells.

This is the signature of the filter:

$data = apply_filters('vg_sheet_editor/load_rows/output', $data, $wp_query_args, $spreadsheet_columns, $clean_data);

What can I do with this filter?

Here are some examples:

1- Modify the URL of the “view” column

The posts spreadsheet has the “view” column with a link to the public post. You can use this code to replace the URL in the button displayed in the “view” column with a link to something else on each row.

add_filter('vg_sheet_editor/load_rows/output', function($data, $wp_query_args) {
    if ($wp_query_args['post_type'] === 'post') {
        foreach ($data as $row_index => $row) {
            if (isset($row['view_post'])) {
                $data[$row_index]['view_post'] = 'http://google.com';
            }
        }
    }
    return $data;
}, 10, 2);

2- Modify the URL of the post editor

The posts spreadsheet has the “WP Editor” column on each row, linking to the normal wp editor. The cell contains the URL of the wp editor.

You can use this filter to change the URL to open a frontend editor or another page where you want to edit this post instead of the wp editor.

Note, in the snippet the variable $row has all the info of the row, including the ID, so you can use it to build a dynamic URL or anything you want.

add_filter('vg_sheet_editor/load_rows/output', function($data, $wp_query_args) {
    if ($wp_query_args['post_type'] === 'post') {
        foreach ($data as $row_index => $row) {
            if (isset($row['open_wp_editor'])) {
                $data[$row_index]['open_wp_editor'] = 'http://link-to-my-other-editor.com';
            }
        }
    }
    return $data;
}, 10, 2);

Important

1- This filter should be used to change aesthetic details (URL of a button or modify a value) but it should not be used to modify the real values of the cell because it might cause confusion to the user.

For example, if you modify the post_title column, the user would see a fake title. The user might run a bulk edit to replace the fake title with another title and the edit will fail because the fake title doesn’t exist.

2- The changes you make with this filter apply to the export tool too. The exported file will have those changes as well as the cells.

3- This filter is for editing the cell values, not the cell renderers or html.

For example, the “view” column has the URL as the value so you can change the URL only. But if you want to change the “view” button or icon of the button, that can’t be changed with this filter because the button html is generated dynamically in other way.

Close Menu