1

In the admin backend we use a query arg that is left in the URL and keeps getting used whether it is supposed to or not.

Our plugin has an array with values that can be toggled via the custom backend page. Against the option that should be toggled there is a link titled 'Activate' or 'Deactivate', pressing that link returns to the same admin page but with an added query arg which is parsed when the page is initialised. The admin page can be reloaded by following the toggle links or pressing the standard 'Save changes' backend button. The problem is that with the query arg remains in the URL when 'Save changes' is pressed, so the value is toggled again without the 'Activate' or 'Deactivate' being followed.

The arg should be removed when 'Save Changes' is pressed. Is there a filter for the 'Save changes' button so we could add our own filter to remove the custom arg?

class Custom_Admin {
private $options;

public function __construct()
{
    $this->options = get_option( 'custom_options' );
    add_filter('query_vars', array($this, 'add_query_vars'));
    add_action('init', array($this, 'check_query_vars') );
}

public function add_query_vars($public_query_vars) {
    $public_query_vars[] = 'code_key';
    return $public_query_vars;
}

public function check_query_vars() {
    $code_key = isset($_GET["code_key"]) ? $_GET["code_key"] : '';

    $active = $this->options[$code_key]['active'];
    $this->options[$code_key]['active'] = ($active == true ? false : true);

    update_option('custom_options', $this->options);
}

public function page_init()
{
    register_setting(
        'group', // Option group
        'options', // Option name
        array( $this, 'sanitize' ) // Sanitize
    );
}

public function sanitize($input)
{
    // Parse and sanitise more stuff here like a normal person
}

}

3 Answers 3

5

This might be useful: there is a filter called removable_query_args. You get an array of argument names to which you can append your own argument. Then WP will take care of removing all of the arguments in the list from the URL.

function add_removable_arg($args) {
    array_push($args, 'my-query-arg');
    return $args;
}

add_filter('removable_query_args', 'add_removable_arg');

Something like:

'...post.php?post=1&my-query-arg=10'

Will become:

'...post.php?post=1'
0

If you are using the Settings API for the Save Changes, you are probably using a nonce field with it (if not it is easy enough to add with wp_nonce_field whether you are using Settings API or not) ...

So testing for the presence of the nonce field is one way to test and help you distiguish between what action should be happening so as to not update the other custom option. eg.

public function check_query_vars() {
    // bug out here if nonce value is set
    if (isset($_REQUEST['_wpnonce'])) {return;}

    $code_key = isset($_GET["code_key"]) ? $_GET["code_key"] : '';

    $active = $this->options[$code_key]['active'];
    $this->options[$code_key]['active'] = ($active == true ? false : true);

    update_option('custom_options', $this->options);
}
3
  • Good idea, let me give it a quick spin... Commented Jul 28, 2016 at 19:51
  • The query var check method is run when the page loads where as the form callback method runs when the button is pressed. I will try a different approach, making each toggle link a form submission with its own vars makes sense rather than struggle with this somewhat old fashioned solution. Commented Jul 28, 2016 at 20:36
  • yeah i agree, it makes more sense to test for the exact needed conditions rather than to try to exclude all other possible ones.
    – majick
    Commented Jul 29, 2016 at 3:50
0

Maybe JS will do what you want? This should be the easiest way to achieve what you want. More info about removing query string param in JS

1
  • I'll use the function in the correct answer in your link. Getting the URL as a String and manipulating it is step one, I can do that, the next step is the one I cannot do, replacing the existing URL when the 'Save changes' button is pressed. I'll do some digging in WordPress and see if there is a filter to hook into. Commented Jul 28, 2016 at 19:43

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