pagination - remove $_GET-parameter from WP_List_Table::tablenav

admin2025-06-02  1

I'm using URL parameters as actions links within a custom WP_List_Table for users like so:

.php?page=account-expiration-status&do_action_xyz&email=email%40domain&_wpnonce=xyz

Within the parent class, I'm checking for the existence of

array_key_exists( 'do_action_xyz', $_GET )

to trigger the appropriate functions.

This works fine, but unfortunately the $_GET-parameters get added to the paginations links of the table as well. This causes the action to get triggered twice when using the tablenav to browse forward or backward in the table.

I've tried removing the $_GET-parameters right after executing the desired action with both:

unset( $_GET['do_action_xyz'] );

and

remove_query_arg( 'do_action_xyz' );

but WordPress still adds them to the pagination links of the table.

Can anyone help me to remove the query arg correctly after executing the action?

I'm using URL parameters as actions links within a custom WP_List_Table for users like so:

http://domain/wp-admin/users.php?page=account-expiration-status&do_action_xyz&email=email%40domain&_wpnonce=xyz

Within the parent class, I'm checking for the existence of

array_key_exists( 'do_action_xyz', $_GET )

to trigger the appropriate functions.

This works fine, but unfortunately the $_GET-parameters get added to the paginations links of the table as well. This causes the action to get triggered twice when using the tablenav to browse forward or backward in the table.

I've tried removing the $_GET-parameters right after executing the desired action with both:

unset( $_GET['do_action_xyz'] );

and

remove_query_arg( 'do_action_xyz' );

but WordPress still adds them to the pagination links of the table.

Can anyone help me to remove the query arg correctly after executing the action?

Share Improve this question asked Jan 21, 2016 at 7:25 user67014user67014 661 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You should consider the POST request method for your action.

Otherwise you might try to hijack the set_url_scheme filter with:

add_filter( 'set_url_scheme', 'wpse_remove_arg' );

function wpse_remove_arg( $url )
{   
    return remove_query_arg( 'do_action_xyz', $url );
}

Then you could try to narrow the scope and only run this on the corresponding table page. Further you could limit it's affect by adding it as close to the table pagination list as possible and then remove it with:

remove_filter( 'set_url_scheme', 'wpse_remove_arg' );

as soon as possible.

I add this filter within my page's load-{$plugin_page} action hook. I add my equivalent of your do_action_xyz parameter to a wp_redirect() which executes after that filter is added, but the redirect URL seems unaffected by it.

I also "remove" the query parameter on the page for which it's used to display a message to the user using the history API.

$param = 'do_action_xyz';
if (isset($_REQUEST[$param])) {
    add_filter('removable_query_args', function($rqa) use ($param) {
        $rqa[] = $param;
        return $rqa;
    });
    add_action('admin_notices', function() use ($param) {
        # show message here

        ?><script>
        if (history.replaceState) {
            history.replaceState(
                {}
                , document.title
                , location.pathname +
                    location.search.replace(/&<?= $param ?>=[^&]+/g, '') +
                    location.hash
            );
        }
        </script>
        <?php
    });
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748859722a314319.html

最新回复(0)