Prevent invalid or empty values from being saved to the database and retain the form field values upon error

You’d do it something like this: function sanitize_number_callback ($input){ if( !preg_match( ‘/…regex for valid here…/’, $input ) ){ add_settings_error( ‘my_option’, esc_attr( ‘my_option’ ), //becomes part of id attribute of error message __( ‘Number must be a positive integer’, ‘wordpress’ ), //default text zone ‘error’ ); $input = get_option( ‘my_option’ ); //keep old value } return … Read more

Settings API with arrays example

Short answer: your name attribute values must use the schema option_name[array_key]. So, when you use … <input name=”option_name[key1]”> <input name=”option_name[key2]”> … you get an array as option value in your validation function: array ( ‘key1’ => ‘some value’, ‘key2’ => ‘some other value’ ) PHP does that for you, this is not a WordPress feature. … Read more

Testing Rewrite Rules with PHPUnit

There are two problems here, a conceptual and practical Conceptional: What is it that you are testing? If you are testing that your function actually inserts the rule into the set of rewrite rules, than you should just inspect the generated rewrite rules array (from the option probably). Anything beyond that is not “unit testing” … Read more