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