Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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

share|improve this question
    
Seeing your code would help here. –  Bainternet Jun 17 '11 at 14:11
    
@Bainternet I don't see how posting my code will help here. The code that runs said functions is not the issue. The issue is that the Settings API submits the GET parameters despite the form being set to submit to options.php. What seems to happen when the settings save is done it redirects back to where it came from, thus running my GET/functions again. –  Brady Jun 17 '11 at 14:30
    
Seeing you could would help understand how you are showing your admin notices and how to keep them upon redirection. –  Bainternet Jun 17 '11 at 15:01
    
@kaiser what's your problem with that? –  Brady Jun 17 '11 at 15:23

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
    
Sorry this wont work as settings_fields("sc_wpfmp_settings"); auto adds _wp_http_referer to the form. –  Brady Jun 17 '11 at 15:31
    
Just add $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'sc_wpfmp_action', 'sc_wpfmp_scan', 'sc_wpfmp_reset_settings' ) ); right before you call it :) –  TheDeadMedic Jun 17 '11 at 15:42
    
Thx TheDeadMedic. This solution works although I feel a bit hacky. I'd like to leave this question open for a bit longer to see if anyone else has any other solutions. –  Brady Jun 17 '11 at 16:28
    
Admittedly it is - you can actually just place the input after the settings field - PHP will always take the 'last' value and override any others. –  TheDeadMedic Jun 17 '11 at 17:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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