I’m looking to sanitize admin form input for update_option. The input is for a directory path and file name. The input will look like this:
/directory/subdirectory/
and
/thisfile.min.js
So far, every sanitize I’ve tried strips out the forward slashes. Any suggestions?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The function that you would want to use is sanitize_text_field() which among other things, strips all HTML tags, and remove line breaks, tabs and extra white space.
// This worked fine for me - I got /directory/subdirectory/
$value = sanitize_text_field( '/directory/subdirectory/' );
// And so does this one - I got /thisfile.min.js
$value = sanitize_text_field( '/thisfile.min.js' );
// Even with an actual form input (e.g. <input name="my_field">),
// the function worked just fine (using the same data as above).
$value = sanitize_text_field( $_POST['my_field'] );
So have you already tried that function and are you sure it stripped the /? How about the other similar functions listed here?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0