最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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
    });
}
Post a comment

comment list (0)

  1. No comments so far