After the seventh time I shouted “stop autocorrecting cmb2 fields!” at my site, I knew I had to do something.
When you run a website where you enter a lot of people’s names, Autocorrect is a curse more than a blessing. Of course I want it on my post content, but when I get to the field where I enter someone’s name, for crying out loud, some names like Nuñez just don’t meet a spell check. And don’t get me started on my friend’s names or my own. I’ve lost track of the number of times I ended up as “Mike.”
This issue used to only be on phones and tablets. Then Apple introduced autocorrect to their MacOS, which resulted in a lot of tweets followed up by “Damn you, Autocorrect!”
HTML Attributes
If you’ve got a form and you want to tell autocorrect to go away, the code looks like this:
<input autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />
For the most part, this will work. You don’t need all of them all of the time, but in my case I was adding names and “Debbie van Houten” was one problem and “Dr. el Farad” was another. I wanted it to just shut up and let me type the name as their parents intended, no matter what. I went whole hog.
But as I mentioned, I use CMB2 and I needed to stop my site from autocorrecting CMB2 fields. It was time for some code.
CMB2 Custom HTML Attributes
This one is so straightforward I was delighted. When you create a new field, you can set arbitrary attributes.
// Field: Actor Name $cmb_characters->add_field( array( 'name' => 'Actor Name', 'desc' => 'Include years (in parens) for multiple actors', 'id' => $prefix . 'actor', 'type' => 'text', 'repeatable' => 'true', 'attributes' => array( 'autocomplete' => 'off', 'autocorrect' => 'off', 'autocapitalize' => 'off', 'spellcheck' => 'false', ), ) );
That was all I needed to do in order to get autocorrect to duck itself. Now I was free to write however weird a name I needed without worrying that autocorrect wanted to call me Mike. Again.
Thanks, autocorrect. Thanks a lot.
Comments
2 responses to “Damn You, Autocorrect!”
Thanks for sharing this great feature Mike! I had no idea that attribute existed.
I didn’t know about the spellcheck attribute. There are a lot of cases I DO wanted spellcheck on a text field