WordPress.org

Ready to get started?Download WordPress

Forums

register_setting and re-setting option values. (18 posts)

  1. Steve
    Member
    Posted 4 years ago #

    I've got an options form set up using register_setting and settings_fields and its all working fine.

    But how do I put in a reset options button? Under the old way I added a button and checked for a $_POST value and could then erase and reset the settings I was storing.

    Using register_setting the $_POST gets intercepted and wiped before I can get to it and $_ACTION contains data but its the same data no matter which button I use.

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Using register_setting the $_POST gets intercepted and wiped before I can get to it

    You don't need to intercept $_POST if you're registering options, the third parameter for register_setting is the callback function used to sanitize the setting, declare the callback and you have control over the sanitizing.

    You could use jQuery to empty the fields using a reset link, assuming it's admin side, jQuery will already be loaded already, so you'd only need a few lines of code.

    Something like..

    jQuery(document).ready(function($) {
    	$('a.yourlinkclass').click( function() {
    		$('input[type="text"]').val('');
    		$('textarea').val('');
    	});
    	return false;
    });
  3. Steve
    Member
    Posted 4 years ago #

    I've already done the santisation I just want to offer an easy reset option which we used to have before the new "improved" system came along - which is why I mentioned $_POST because you used to be able to grab that, check a value and then simply reset the settings back to a known state by basically using the update_option call.

    Are you really telling me that I've now got to go and start writing javascript just to reset some values in a form? Isn't that rather an obscure way of doing it? I really would have hoped that there was some rather more integrated WP/PHP way of doing it.

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    I only suggested it because it was how i had dealt with a resetting option for something i just did. I'm sure there is an easier way, but seeing how i don't do a whole lot of plugin/mod writing i can't say for sure what the easiest way is.

    I'm just a user like yourself, don't take what i say as an absolute answer. Can you clarify what you mean by reset exactly, reset as in empty some fields of their values, or reset as in set all the fields to some default values?

    If you can give me an example of somewhere you've seen a reset option in WordPress i can probably go the one step further a dig up how it's done.. (in the spirit of being helpful)... ;)

  5. Steve
    Member
    Posted 4 years ago #

    Basically I've got a complex plugin and sometimes the users mess up their settings. So I want a simple way for them to basically reset the settings to the default set I ship it with which work.

    I cant say exactly where I've seen an option reset inside WordPress (apart from the fact that I could do it using the "old" method)

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    How were you doing it before(old method)?

    As far as loading a set of default options, i can't see part that being too hard..

    update_option( 'your_option' , $array_with_default_values );

    What are you expecting in terms of how the reset will function, a page refresh, where the options are filled out with defaults, or a non-refreshing type action that fills the fields in with default values..

  7. Steve
    Member
    Posted 4 years ago #

    I was doing it by picking up a specific value in $_POST and basically removing the option set.

    I'd already got code that checked for the option set and if it was missing it added it so by doing that I could simply give the user the option to reset the options to the defaults, and if it existed (i.e. the user had set the options) it didn't do anything

    The options form is populated from the option set so that seemed a sort of logical way to do it.

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    Well there's a few ways you can run a reset routine, it really depends what you want it to do.

    Are you checking user input, and if something is missing, then set some defaults, or... if it's the plugin's first load, then apply default values..

    If i can understand more about the behaviour you want and/or expect i can help suggest some ideas..

    If you're wanting to check at submission of options, then you can be doing the necessary checks inside the sanitizing function..

    Assuming for a moment this was a callback function to sanitize some plugin options.

    function sanit_options( $input ) {
    	if( !$input )
    		return $defaults; // $defaults could be an array of some default values.
    	if( $input['someoption'] && $input['someoption'] == '' )
    		$input['someoption'] = 'some default value to replace empty string if found';
    
    	// Assuming you have a group/array of options
    	foreach( $input as $option => $option_value ) {
    		// Do sanitizing stuff... etc..
    	}
    
    	// Return the input / data set
    	return $input;
    }

    A very bare example, but all i'm showing you above is how to conditionalise around the submission of data, it's essentially getting done in the callback function, because this the only place you'll have access to the submitted data (since you form will be submitting it's data to the options page, not your plugin page).

  9. Steve
    Member
    Posted 4 years ago #

    I've got all the code in place to set defaults the first time round and to allow user to change settings and save them. I simply want an easy way for a user to basically scrap all their customisation and reset things back to the default without them having to do anything drastic like de-install the plugin.

  10. Mark / t31os
    Moderator
    Posted 4 years ago #

    You could use a $_GET or $_REQUEST method to handle setting up defaults..

    yourpluginurl/?reset=true

    Or whatever... just a standard query string type thing..

    As long as your plugin receives that request it should be fairly straight-forward. I can help you with code to do that if necessary.

  11. Steve
    Member
    Posted 4 years ago #

    I'm capable of coding the code to handle changing things - that's not a problem - and in fact I've got that in place using the "old" version where you had to do all the sanitization and processing yourself.

    The problem is that the minute you move to the new method of doing it and your form starts:

    echo'<form action="options.php" method="post">';
    settings_fields('wordbooker_options');

    and ends:

    echo '<input type="submit" value="Save Blog Level Options" class="button-primary" />&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="Reset to system Defaults" class="button-primary" /></form>';

    then $_GET and $_REQUEST contain the following when you go into the page the first time:

    array(1) { ["page"]=> string(10) "wordbooker" }

    and if you use either of the submit buttons (i.e. save or reset) then the $_GET and $_REQUEST contain:

    array(2) { ["page"]=> string(10) "wordbooker" ["updated"]=> string(4) "true" }

    and $_POST is empty all the time (whereas using the old method you could do a check for $_POST["Reset to system Defaults"])

    so you still cannot determine which button has been pressed.

    I suppose I could do it with a second form just for the reset button but it seems silly.

  12. Steve
    Member
    Posted 4 years ago #

    It looks like what you have to do is catch the reset in the validation routine

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    Yes because your input gets sent to the options page it's not around to look at..

    So yes, agreed you'd have to deal with it in the validation routine, by setting an additional option... it could be the sake of setting a value to 0 or 1, then performing a reset if you check against the value of this option on displaying / outputting your plugin page..

    I tend to have a function that returns an array of default values, when i need to reset the values to defaults i return the defaults via function call inside the validation process..

    if( $some_input['key'] == 'something' )
      return self::default_values();

    self refers to the class ... default_values the function name, a function that simply returns an array of key/value pairs... (defaults)...

  14. Steve
    Member
    Posted 4 years ago #

    I did it by adding:
    if ( ($_POST["submit"]=='Reset to system Defaults')) { set_default_options() ; }

    into the top of my validation routine.

  15. Mark / t31os
    Moderator
    Posted 4 years ago #

    Good stuff... :)

    You could wrap that in an isset check (or add inside the condition), else you'll possibly chuck up a warning if the value for whatever reason is not set..

    if ( isset( $_POST["submit"] ) && ( $_POST["submit"] == 'Reset to system Defaults' ) ) {
  16. Steve
    Member
    Posted 4 years ago #

    That's true - and I will now I've got it working without it doing any odd things.

    I found that if I deleted the options inside the validation using the built in WordPress function it segfaulted apache!

  17. Mark / t31os
    Moderator
    Posted 4 years ago #

    The array of values should be available inside the callback function, if you want to return nothing, then do that literally..

    return;

    ..as appose to returning the new array or a set of defaults..
    ie.

    return $defaults;

    or

    return $incoming_vars;

    When i'm refering to incoming vars, i mean the vars that get passed to the callback..

    function some_callback_name( $incoming_vars ) {

    Which is what you should be running over and sanitizing ... not $_POST ..

    Any case, happy you were able to get to what you wanted... :)

  18. Steve
    Member
    Posted 4 years ago #

    I know I don't sanitize $_POST - its just that a submit button puts stuff into $_POST and that's easier than having the user check a specific option to indicate they want to reset data and then save the settings.

    So I check the $_POST['submit'] variable (which tells me which button they've clicked on) and then I replace the relevant elements of the $options array with the default values and then return the array.

Topic Closed

This topic has been closed to new replies.

About this Topic