Developer quick start
Nahnu Login Shield gives developers a public API: PHP functions, hooks, REST routes and a JavaScript object. You can use it from a theme, a page-builder template, a custom plugin, or a headless front end. This topic shows the shortest path to each of the main uses and points you to the full reference for the rest.
What the API covers#
| Layer | What you use it for | Where to read more |
|---|---|---|
| PHP functions | Protect a custom form, check IPs and lockouts, write to the log, read settings | Functions reference |
| PHP hooks (actions and filters) | React to a blocked or passed submission, bypass a check, change widget output | Actions and filters |
| Passwordless PHP tags and shortcodes | Add a passkey button or a magic-link form to any page | Passwordless API |
REST routes at /wp-json/nahnls/v1/ | Headless and mobile sign-in, passkeys, 2FA | REST API |
JavaScript window.NAHNLSAuth | Passkey, magic-link and 2FA calls from the browser | JavaScript API (NAHNLSAuth) |
JavaScript window.NAHNLSWidget | CAPTCHA widget in forms built after page load | CAPTCHA widget in JavaScript forms |
| CSS and filters for the account panel | Style the Login & security panel and the WooCommerce challenge | Front-end theming |
Before you start#
- The plugin must be active. Every function name starts with
nahnls_. - Always guard your calls with
function_exists()so your code keeps working if the plugin is ever deactivated. - Call the functions on
initor later, or from a template. They are not meant to run while WordPress is still loading plugins. - The features you call must be turned on in the plugin’s settings. Calls for a feature that is off return an empty string,
falseor aWP_Error, so it’s safe to call them unconditionally. - Passkeys need HTTPS (or
localhost). - The plugin needs WordPress 6.2 or newer and PHP 8.3 or newer.
Example 1: Protect a custom form#
Two calls: put the widget in the form, and check the submission in your handler.
<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> 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_widget_html() prints the widget for the CAPTCHA provider chosen on the Widget tab, plus the hidden security fields. nahnls_check_submission() returns true on success or a WP_Error with a message you can show to the visitor.
Details and edge cases (the form nonce, AJAX forms, cached pages) are in Protect any other form and Functions reference.
Example 2: Add a passkey button and a magic-link form#
if ( function_exists( 'nahnls_passkey_button' ) ) {
nahnls_passkey_button( [
'label' => __( 'Use a passkey' ),
'redirect' => home_url( '/members/' ),
'echo' => true,
] );
}
if ( function_exists( 'nahnls_magic_link_form' ) ) {
nahnls_magic_link_form( [
'redirect' => home_url( '/members/' ),
'echo' => true,
] );
} In a page builder, use the shortcodes in a Shortcode or HTML element instead:
[nahnls_passkey_button redirect="/members/"]
[nahnls_magic_link_form redirect="/members/"] Passkeys and magic links are separate switches on the Passwordless tab. If a switch is off, the function prints nothing. See Passwordless API.
Example 3: Check which features are on#
Use nahnls_feature_enabled() rather than reading raw settings, because setting names can change between versions.
if ( function_exists( 'nahnls_feature_enabled' ) && nahnls_feature_enabled( 'passkeys' ) ) {
// Passkeys are on and the page is served over HTTPS.
} Feature names you can pass: two_factor, passkeys, magic_link, login_protection, wc_login_protection, account_panel.
Example 4: React to Shield with hooks#
Bypass the check for trusted users:
add_filter( 'nahnls_check_submission_enabled', function ( bool $enabled, array $context ): bool {
if ( current_user_can( 'edit_posts' ) ) {
return false;
}
return $enabled;
}, 10, 2 ); Write your own events to the Log tab:
add_action( 'my_plugin_fraud_detected', function ( string $ip, string $user ) {
nahnls_log_event( 'custom_block', 'Fraud detected by My Plugin', $ip, $user );
} ); The full list is in Actions and filters.
Example 5: Sign in from JavaScript#
On a page where nahnls-auth.js is loaded, the NAHNLSAuth object offers promise-based calls:
NAHNLSAuth.passkeyLogin({ redirect: '/members/' })
.then(({ redirect }) => window.location.assign(redirect))
.catch((e) => console.error(e.code, e.message)); Request a magic link:
NAHNLSAuth.requestMagicLink('user@example.com', '/members/'); Events fire on document, for example:
document.addEventListener('nahnls:passkey:login', function (e) {
console.log('Signed in', e.detail.user_id);
}); For other pages, enqueue the script yourself. See JavaScript API (NAHNLSAuth).
Example 6: Use the REST API#
The REST routes live under /wp-json/nahnls/v1/. Public routes (passkey sign-in, magic-link requests, 2FA steps) carry the same lockout, IP block and geo-block rules as the normal login. Routes that manage a signed-in user’s passkeys need the login cookie plus an X-WP-Nonce header, or an application password.
POST /wp-json/nahnls/v1/magic-link/request { "identifier": "user@example.com" }
GET /wp-json/nahnls/v1/me/security (signed in) See REST API for every route.
Check the API version#
The constant NAHNLS_API_VERSION is the level of the public API. It rises whenever a public function, route or JavaScript method is added, and it is separate from the plugin’s version number. Currently it is 2. Anything marked “API 2” in the docs needs level 2 or later.
if ( defined( 'NAHNLS_API_VERSION' ) && NAHNLS_API_VERSION >= 2 ) {
// nahnls_feature_enabled(), nahnls_user_requires_2fa(), nahnls_start_2fa_challenge() ...
} In JavaScript, NAHNLSAuth.apiVersion holds the same value.
Good habits#
- Guard every call with
function_exists(). - Don’t read the settings option directly. Use
nahnls_get_settings(), ornahnls_feature_enabled()for on/off checks. - Use hooks and the public functions, not internal classes. Class names and file layouts can change between releases.
- Keep protected forms off long-lived page caches, or lengthen the nonce life with the
nahnls_form_nonce_lifefilter. See Caching and login pages. - Test in a private window with your CAPTCHA provider enabled, so you see what visitors see.
Where to read next#
- Functions reference for every function with its parameters and return values.
- Actions and filters for the hooks.
- Passwordless API, REST API and JavaScript API (NAHNLSAuth) for sign-in work.
- CAPTCHA widget in JavaScript forms for forms built after page load.
- Code examples for ready-made snippets.
If you need something the API doesn’t offer, ask for it through the Support page. See Protect any other form.
Was this helpful?
Thanks for your feedback!
✓ Feedback received. Thank you!