Yesterday while working on our brand new WordPress Plugin I noticed this weird white space issue in textarea HTML field
.
After saving settings in Plugin Admin area, during refresh it started appearing. And after every setting save time, it was adding more and more white space.
Do you have any of below questions then you are at right place:
- Why is textarea filled with mysterious white spaces?
- trim() function not working in wordpress
- How to Remove multiple whitespace from textarea after form submit?
- How to Sanitize textarea instead of html input field?
Well, it was really weird as I was sanitizing text area same as input HTML field
but nothing worked.
This is how my TextArea filed was before:
<td> <label for="crunchify"></label> <textarea cols="60" rows="5" name="crunchify[header]" id="crunchify"><?php echo trim(crunchify_opts['header'] ); ?> </textarea> </td>
Initially I thought it’s due to the way I was sanitizing textarea values but nope. That wasn’t the case.
trim():
Strip whitespace (or other characters) from the beginning and end of a string. In my case it worked well.
In my case I was adding the extra spaces after opening
textarea
. Hmm. That was the case.
Best practice for textarea is to close it immediately on the same line
where it started.
I resolved my issue after converting above textarea code and closing it on the same line.
This is what I changed to:
<td> <label for="crunchify"></label><textarea cols="60" rows="5" name="crunchify[header]" id="crunchify"><?php echo trim( crunchify_opts['header'] ); ?></textarea> </td>
Why this happened?
At Crunchify, we are using IntelliJ's PHPStorm
for our plugin development. When you format code then it automatically formats code and add whitespace for text area.
I have modified some of our PhpStorm HTML settings and now resolved this issue permanently.
If you are using PhpStorm then
- Go to PhpStorm Menu
- Click on Preferences…
- Go to Editor
- Go to Code Style
- Go to HTML
- and modified below settings.
Let me know if you have any issue with this setting and I’m more than happy to check that for you.
Between something new is coming
in few weeks. We are working on brand new WordPress Optimizer, Customizer, Beautifier plugin. Please stay tuned.