Skip to content

Instantly share code, notes, and snippets.

@Qubadi
Last active January 28, 2025 17:34
Show Gist options
  • Save Qubadi/0ec7851e0621334de5a2aee595216510 to your computer and use it in GitHub Desktop.
Save Qubadi/0ec7851e0621334de5a2aee595216510 to your computer and use it in GitHub Desktop.
Custom code to disable auto-fill for JetFormBuilder fields ( Disable autofill for all fields in all forms )
UPDATED: 28.01.2025
This custom code is designed to effectively prevent auto-fill on JetFormBuilder forms, as well as other forms,
by dynamically altering the name attributes of input fields. When the page loads, it temporarily changes the name
attributes of visible inputs, text areas, and selects, and sets autocomplete to new-password to stop browsers from
auto-filling the fields.
During form submission, the original names are restored for accurate data handling. After submission, the name
attributes are randomized again to ensure auto-fill remains disabled in future interactions. This approach is ideal
for situations where you need to prevent unintended auto-completion by browsers while still preserving the correct
field names for form processing.
Copy the following PHP code and create a PHP snippet using your snippet plugins.
Paste the code into the plugin and save it.
______________________________________________
add_action('wp_footer', function() {
echo '<script>
document.addEventListener("DOMContentLoaded", function() {
// Select all forms on the page
const forms = document.querySelectorAll("form");
forms.forEach(form => {
// Target all input fields and textareas
const inputs = form.querySelectorAll("input, textarea");
const originalNames = new Map();
inputs.forEach((input, index) => {
// Store original name before modification
originalNames.set(input, input.getAttribute("name") || "");
// Generate a randomized name to block autofill functionality
input.setAttribute("name", `form-field-${index}-${Math.random().toString(36).substr(2, 5)}`);
// Set additional attributes to prevent autofill
input.setAttribute("autocomplete", "new-password"); // Alternative to "off"
input.setAttribute("autocorrect", "off");
input.setAttribute("spellcheck", "false");
input.setAttribute("autocapitalize", "off");
input.setAttribute("aria-autocomplete", "none");
// Apply readonly trick to initially disable autofill, removed on focus
input.setAttribute("readonly", "true");
input.addEventListener("focus", function () {
input.removeAttribute("readonly");
});
});
form.addEventListener("submit", async function(event) {
// Restore original names before submitting the form
originalNames.forEach((originalName, input) => {
input.setAttribute("name", originalName);
});
// Wait before modifying names again to ensure form submission completes
await new Promise(resolve => setTimeout(resolve, 100));
// Reapply randomized names for continued autofill prevention
inputs.forEach((input, index) => {
input.setAttribute("name", `form-field-${index}-${Math.random().toString(36).substr(2, 5)}`);
});
});
});
});
</script>';
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment