Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Aharon Achildiev

Change column set/layout programmatically

Hello,
I’ve configured 2 layouts for the products list.
I want to change the layout based on a query string/GET parameter when viewing the screen.
For example, when filtering based on a post status field (which then I have a GET parameter of “post_status”), I want to set a layout with the ID X, otherwise – set the layout with the ID Y.
My website has many users using the admin interface, so I prefer they will not choose the layout by themselves.
Can you guide me on how to.” do that? Didn’t find any topic or documentation on your site.
I have the pro version, btw.

Thanks,
Aharon

6 days, 1 hour ago
Stefan
Developer

Hello Aharon,

Our plugin does not have a feature for this out-of-the-box and I don’t see an easy way to do this unless you check the URL yourself and do a manual redirect. Aside from the default WordPress filters, our filters (dropdown or Smart Filtering) will only work if the related column is also available for the current layout.

The post-status filter is a default WordPress filter though, so it might work in that case. You’ll need to redirect to a page where the layout_id is also in the URL. layout={your_layout_id
The layout ID can be found in the URL when switching to another layout on the table screen by looking at the URL.

So to help you write something for that, I would do the following

1) write your own logic on the init hook
2) Check if the post_status param is in the URL and you’re on the desired page
3) Add the layout ID to the URL and redirect the page

5 days, 13 hours ago
Aharon Achildiev

Thank you :)

I’m sharing my code with anyone who interested:

add_action( 'admin_init', 'admincolumns_redirect_layout' );

function admincolumns_redirect_layout() {
    $post_type = 'product'; // can be changed to any post type you'd like
    $param = 'post_status'; // put here any GET parameter
    $value = 'value'; // value to check against

    if ( strpos($_SERVER['REQUEST_URI'], 'edit.php') !== false && ($_GET['post_type'] ?? '') === $post_type && !isset($_GET['layout']) ) {
        if ( ($_GET[ $param ] ?? '') === $value ) {
            $layout = 'LAYOUT_ID_1';
        } else {
            $layout = 'LAYOUT_ID_2';
        }

        wp_safe_redirect( add_query_arg( 'layout', $layout ) );
        exit;
    }
}
5 days, 9 hours ago

You must be logged in to reply to this topic.