GET parameters interfere with my plugin settings

I have my plugin all set-up using the Settings API and working but what I have is two manual functions in my plugin that can be run by the admin user by visiting the following URL’s

<?php echo admin_url(); ?>options-general.php?page=wordpress-file-monitor-plus&sc_wpfmp_action=1&sc_wpfmp_scan=1
<?php echo admin_url(); ?>options-general.php?page=wordpress-file-monitor-plus&sc_wpfmp_action=1&sc_wpfmp_reset_settings=1

I’m using the admin_init hook to lookout for these GET parameters and do those functions.

Read More

The functions run fine and the user is back on my plugin settings page but the GET parameters are still in the URL. Not a big issue but if the user then goes ahead and save the settings those GET parameters are sent again and thus running those functions again.

Why does the Settings API send those parameters when submitting the settings form? The action of the form is to submit to options.php.

The only way around my issue I can think of is that after those manual functions have run its code in admin_init is to run a redirect to the settings page without the GET parameters, but if I do this I will lose my admin notices I’m trying to show to the user.

Anyway got any suggestions on how I can get around this problem. Maybe you think there is a better way to run these manual functions?

EDIT: full settings code: http://pastebin.com/Gk5RF5Lc

Related posts

Leave a Reply

1 comment

  1. Why does the Settings API send those parameters when submitting the settings form?

    Quite frankly, it doesn’t. As you already mentioned, your form simply POST’s to options.php, which in turn handles the request, updates the database, and then redirects back to the referer.

    How the referer is fetched is down to the function wp_get_referer();

    function wp_get_referer() {
        $ref = '';
        if ( ! empty( $_REQUEST['_wp_http_referer'] ) )
            $ref = $_REQUEST['_wp_http_referer'];
        else if ( ! empty( $_SERVER['HTTP_REFERER'] ) )
            $ref = $_SERVER['HTTP_REFERER'];
    
        if ( $ref !== $_SERVER['REQUEST_URI'] )
            return $ref;
        return false;
    }
    

    That’s why you’re getting sent back to your options page with the action parameters still there – because they were present in $_SERVER['HTTP_REFERER'].

    But you’ll also see that you can override this behaviour, by placing a hidden input inside your form like so;

    <input type="hidden" name="_wp_http_referer" value="<?php echo admin_url( 'options-general.php?page=wordpress-file-monitor-plus' ); ?>" />
    

    Now options.php will always redirect back to…

    http://example.com/wp-admin/options-general.php?page=wordpress-file-monitor-plus

    …regardless of whatever was in the query string previously.