Protect any other form
This topic covers everything else: a contact form in your theme, a membership plugin’s sign-up form, a custom login drawer, an AJAX form. It also explains how to ask us to add an integration.
In this topic
- Which forms Shield can and can’t protect
- Ask for an integration
- Protect a form yourself with two function calls
- Forms built with JavaScript or loaded over AJAX
- Make your own code react to Shield (hooks)
- Cached pages
- Test your form
Which forms can Shield protect?#
Shield protects a form by adding its verification step (the JavaScript check and your CAPTCHA provider) and then checking the result on the server when the form is submitted. That works in three ways:
| Your form is… | What to do |
|---|---|
| WooCommerce, Bricks Builder, SureForms, or WordPress comments | Turn it on in the Integrations tab. See those topics. |
| Made by another plugin that Shield doesn’t support yet | Ask for an integration (below). In the meantime, a developer can wire it up with the API. |
| Your own code (a custom theme form, a small plugin, a custom endpoint) | Use the API yourself. Two function calls are enough. |
Forms Shield can’t protect: forms that are embedded from another service and submitted to that service’s servers, not to your site. A HubSpot form, or a form pasted in as a JavaScript embed from a CRM or email provider, never passes through WordPress, so Shield never sees the submission. Use that service’s own spam protection for those forms.
Ask for an integration#
Shield is a free plugin. We can’t support every form plugin, but we add as many as we can, and requests from real sites decide what comes next.
To ask for one:
- Open Settings → Login Shield → Integrations. The blue note above the form tiles has a link, submit a request on nahnuloginshield.com. (You can also use Request an integration on the About tab.)
- On the Support page, choose the integration request option and send the form.
- In your request, include:
- The name of the form plugin and a link to it (WordPress.org or the vendor’s site).
- Whether it’s the free or the paid version, and which version number.
- What you want protected: one form, all forms, a login form, a registration form?
- Whether the form submits normally, over AJAX, or over the REST API, if you know.
- Your Shield version (shown on the About tab) and your CAPTCHA provider.
Requests are read by a person and are not guaranteed, because this is a free plugin. The more popular a plugin is, and the more cleanly it lets other plugins hook in, the easier it is to add. Please use our contact form here: https://www.nahnuloginshield.com/support/#integration
Protect a form yourself (developers)#
If you or your developer can edit PHP, you can protect any form with two function calls. Nothing needs to be registered and no classes need to be created.
Step 1: Put the widget inside your form, before the submit button:
<form method="post">
<input type="text" name="your_name">
<input type="email" name="your_email">
<textarea name="message"></textarea>
<?php
if ( function_exists( 'nahnls_widget_html' ) ) {
echo nahnls_widget_html( [ 'form_id' => 'contact' ] ); // phpcs:ignore
}
?>
<button type="submit">Send</button>
</form> nahnls_widget_html() prints the widget for the CAPTCHA provider you chose on the Widget tab. It also adds the hidden security fields (the form nonce and the JavaScript check) and loads the scripts and styles it needs. If your provider is set to None, it prints only the hidden fields.
Step 2: Check the submission in the code that handles the POST:
add_action( 'init', function () {
if ( empty( $_POST['your_name'] ) ) {
return;
}
if ( function_exists( 'nahnls_check_submission' ) ) {
$result = nahnls_check_submission( 'contact' );
if ( is_wp_error( $result ) ) {
wp_die( esc_html( $result->get_error_message() ) );
}
}
// The submission passed. Process the form here.
} ); nahnls_check_submission() returns true when the submission passes, or a WP_Error when it is blocked. The error message is written for visitors, so you can show it as is.
Notes
- Always wrap both calls in
function_exists(). Your site then keeps working if Shield is ever deactivated. - Use the same
form_idin both calls. It’s just a label that identifies the form. - The hidden form nonce is required. If you print your own CAPTCHA markup instead of
nahnls_widget_html(), addnahnls_form_nonce_field()inside the<form>. Without a valid nonce, the check fails with “Security check expired. Please reload the page and try again.” - Shield’s CAPTCHA settings apply to your form as well: the provider and keys are the ones on the Widget tab, and the JavaScript presence check is the one on the General tab.
Other helper functions
| Function | What it does |
|---|---|
nahnls_get_ip() | The visitor’s real IP address, using your proxy/CDN header setting. |
nahnls_is_locked_out( $ip ) | Whether the IP is currently locked out. |
nahnls_is_ip_blocked( $ip ) | Whether the IP is on the blocklist. |
nahnls_is_widget_active() | Whether a CAPTCHA provider is set (not “None”). |
nahnls_get_country() | The visitor’s two-letter country code, or null if it can’t be found. |
nahnls_get_settings() | All Shield settings. |
nahnls_log_event( $event, $detail, $ip, $username ) | Writes your own entry to Shield’s event log. |
For the complete reference with every parameter and return value, see Developer API → Functions reference.
Forms built with JavaScript or loaded over AJAX#
Some forms don’t exist on the page when it first loads: a login drawer, a modal, or a form fetched by AJAX. Shield can put its widget into these too.
- When your AJAX handler builds the form, ask for the widget in AJAX mode:
$widget = nahnls_widget_html( [ 'form_id' => 'nav_login', 'context' => 'ajax' ] );
wp_send_json_success( [
'html' => $form_html . wp_kses( $widget, nahnls_markup_allowed_html() ),
] ); - On the normal page load, make sure Shield’s widget script is on the page:
add_action( 'wp_enqueue_scripts', function () {
if ( function_exists( 'nahnls_enqueue_widget_assets' ) ) {
nahnls_enqueue_widget_assets();
}
} ); - After you insert the form into the page, start the widget:
drawerBody.innerHTML = formHtml;
window.NAHNLSWidget.init( drawerBody ); - After a failed attempt, reset the widget before the visitor retries:
window.NAHNLSWidget.reset( drawerBody ); - In the handler that receives the submission, call
nahnls_check_submission( 'nav_login' )exactly as in Step 2 above.
See Developer API → CAPTCHA widget in JavaScript forms for the full list of JavaScript methods and what each provider does.
Make your own code react to Shield (hooks)#
Shield gives you hooks around nahnls_check_submission(), so your plugin can log, count or change what happens.
Actions
| Hook | When it runs |
|---|---|
nahnls_before_custom_check | Before the checks start. |
nahnls_after_custom_check | After the checks, whether they passed or not. |
nahnls_custom_check_blocked | Only when a submission is blocked. You receive the error. |
nahnls_custom_check_passed | Only when a submission passes. |
Filters
| Hook | What you can do |
|---|---|
nahnls_check_submission_enabled | Return false to skip the check for a particular form. |
nahnls_widget_html_args | Change the widget’s arguments before it is built. |
nahnls_widget_html_output | Change the widget’s final HTML. |
nahnls_form_nonce_life | Change how long the hidden form nonce stays valid, in seconds (default: one day). |
Every action receives a $context array with the form_id, the page address (post_url) and the active provider.
Example: skip the check for editors
add_filter( 'nahnls_check_submission_enabled', function ( bool $enabled, array $context ): bool {
if ( current_user_can( 'edit_posts' ) ) {
return false;
}
return $enabled;
}, 10, 2 ); Example: write blocked submissions to the Shield log
add_action( 'nahnls_custom_check_blocked', function ( WP_Error $error, array $context ) {
nahnls_log_event(
'captcha_fail',
$context['form_id'] . ' -- ' . $error->get_error_code()
);
}, 10, 2 ); Example: log your own event
add_action( 'my_plugin_fraud_detected', function ( string $ip, string $user ) {
nahnls_log_event( 'custom_block', 'Fraud detected by My Plugin', $ip, $user );
} ); Your entries appear in the Log tab with the other Shield events. Event names are limited to 32 characters and details to 255.
Example: protect a custom REST endpoint from locked-out IPs
register_rest_route( 'my-plugin/v1', '/submit', [
'methods' => 'POST',
'callback' => 'my_plugin_handle_submit',
'permission_callback' => function () {
if ( function_exists( 'nahnls_is_locked_out' ) && nahnls_is_locked_out() ) {
return new WP_Error( 'locked', 'Too many failed attempts.', [ 'status' => 429 ] );
}
return true;
},
] ); Hooks for SureForms (nahnls_sureforms_form_enabled), the passwordless features and the email templates are covered in their own topics and in the Developer API category.
Cached pages#
The hidden form nonce is valid for about a day. If a page with a protected form is stored in a full-page cache for longer than that, visitors who load the cached copy may get “Security check expired” when they submit.
- Exclude pages with protected forms from full-page caching, or
- Keep the cache lifetime shorter than a day, or
- Lengthen the nonce life with the
nahnls_form_nonce_lifefilter:
add_filter( 'nahnls_form_nonce_life', fn() => 2 * DAY_IN_SECONDS ); When JavaScript runs on the page, Shield refreshes the nonce on load, which already helps with most cached pages. See Troubleshooting → Caching and login pages.
Test your form#
- Open the page in a private window (so you are logged out) and confirm the widget appears.
- Submit the form normally. It should go through.
- Submit it without completing the CAPTCHA, if your provider shows one. It should be refused with a message.
- Temporarily turn off JavaScript in your browser and submit. If the JavaScript presence check is on, it should be refused.
- If you added the logging hook above, open the Log tab and confirm the entry appeared.
Was this helpful?
Thanks for your feedback!
✓ Feedback received. Thank you!