0

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

http://domain.com/wp-admin/users.php?page=account-expiration-status&do_action_xyz&email=email%40domain.com&_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?

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.

0

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
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.