Press "/" to Jump to the Search Box
Lets visitors press the slash key on any page to put the cursor in the search box, a shortcut many people know from popular websites.
Opens WPIDE on
Code
document.addEventListener('keydown', function (event) {
if (event.key !== '/' || event.ctrlKey || event.metaKey || event.altKey) {
return;
}
var active = document.activeElement;
if (active && (active.isContentEditable || /^(INPUT|TEXTAREA|SELECT)$/.test(active.tagName))) {
return;
}
var field = document.querySelector('input[type="search"], input[name="s"]');
if (field) {
event.preventDefault();
field.focus();
field.select();
}
});
Notes
The shortcut is ignored while the visitor is typing in a form field.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.