Passwordless API
The Passwordless API lets you add passkey sign-in, emailed login links (magic links) and the Login & security panel to any page: a theme template, a page-builder form, a membership plugin’s login screen, or a headless app. This topic covers the PHP side (template tags, shortcodes, helpers) and shows ready-made recipes. The browser and server interfaces have their own topics: JavaScript API (NAHNLSAuth) and REST API.
For what passkeys and magic links are and how to switch them on, see Passkeys and Magic links.
How the pieces fit together#
There are three layers, all backed by the same server code:
| Layer | Use it from | Entry points |
|---|---|---|
| PHP template tags and shortcodes | Themes, page-builder templates, other plugins | nahnls_passkey_button(), nahnls_magic_link_form(), nahnls_send_magic_link() and others |
REST routes at /wp-json/nahnls/v1/ | Headless, React and mobile front ends | passkeys/login/*, magic-link/request, 2fa/*, passkeys, me/security |
JavaScript window.NAHNLSAuth | Any page where nahnls-auth.js is loaded | passkeyLogin(), passkeyRegister(), requestMagicLink(), twoFactorVerify() and others |
Before you start#
- Passkeys and magic links are separate switches on the Passwordless tab. Switch on the ones you want to use.
- Every function returns
'',falseor aWP_Errorwhen its feature is off, so you can call it without checking first. - Passkeys need HTTPS (or
localhost). On a page that isn’t HTTPS,nahnls_passkey_button()prints nothing. - The Limit passwordless sign-in by role setting still applies. A user in a role that isn’t allowed can’t use the feature, whatever your code does. See Limit passwordless sign-in by role.
- Passkey and magic-link sign-ins go through the same hard stops as the normal login: IP blocklist, lockouts and geo-blocking. If two-factor authentication applies to the user, it still applies after a passkey or a magic link, unless the passkey itself counts as the user’s second factor.
- Guard your calls with
function_exists().
Add a passkey button#
A form that posts a password to wp-login.php must carry the plugin’s hidden fields, nahnls_js and nahnls_form_nonce. With Require JavaScript set to Strict, a password login without them is refused. With a CAPTCHA provider switched on, the login also needs the widget. nahnls_widget_html() prints the widget and both fields:
<form method="post" action="<?php echo esc_url( wp_login_url() ); ?>">
<input type="text" name="log" autocomplete="username webauthn">
<input type="password" name="pwd">
<label><input type="checkbox" name="rememberme" value="forever"> Remember me</label>
<?php
if ( function_exists( 'nahnls_widget_html' ) ) {
echo nahnls_widget_html( [ 'form_id' => 'login' ] ); // phpcs:ignore
}
?>
<button type="submit">Log in</button>
<?php
if ( function_exists( 'nahnls_passkey_button' ) ) {
echo nahnls_passkey_button( [ 'redirect' => home_url( '/account/' ) ] ); // phpcs:ignore
}
?>
</form> nahnls_passkey_button( $args ) returns the button markup and loads its scripts, so it works from a template, a shortcode or a widget.
| Argument | Default | What it does |
|---|---|---|
label | “Sign in with a passkey” | Button text |
class | '' | Extra CSS classes on the button |
redirect | redirect_to or the dashboard | Where the user lands after signing in |
echo | false | Print instead of return |
The button reads the form’s “Remember me” checkbox (rememberme), if there is one.
Shortcode:
[nahnls_passkey_button label="Use a passkey" redirect="/account/" class="btn"] autocomplete="username webauthn" on the username field lets browsers offer a saved passkey in the autofill list.
Add a magic-link request#
You have two choices.
Link to the built-in page:
<a href="<?php echo esc_url( nahnls_magic_link_url( home_url( '/account/' ) ) ); ?>">Email me a login link</a> nahnls_magic_link_url( $redirect ) returns the address of the “Email me a login link” page on the login screen, or '' when magic links are off.
Print a small form on your own page:
if ( function_exists( 'nahnls_magic_link_form' ) ) {
nahnls_magic_link_form( [
'redirect' => home_url( '/members/' ),
'submit' => 'Send me a link',
'echo' => true,
] );
} When login protection is on, the form includes the JavaScript marker and the CAPTCHA widget, so a request passes the same checks as the login page. After the visitor submits, they see the neutral “Check your inbox” page. That page doesn’t reveal whether the account exists.
| Argument | Default | What it does |
|---|---|---|
redirect | '' | Where the emailed link lands |
label | “Email or username” | Field label |
placeholder | '' | Input placeholder |
submit | “Send login link” | Button text |
class | '' | Extra classes on the form |
input_class, button_class | nahnls-ml-input, nahnls-ml-submit | Classes for your CSS |
echo | false | Print instead of return |
Shortcode:
[nahnls_magic_link_form redirect="/account/" submit="Send me a link"] Send a magic link from code#
Use nahnls_send_magic_link( $user, $redirect, $args ) for invitations, “log in as” tools and support flows. It follows the role allow-list and the user’s opt-out. The per-account rate limit also applies, unless you pass force. The token itself is never returned.
add_action( 'my_plugin_member_approved', function ( int $user_id ) {
if ( ! function_exists( 'nahnls_send_magic_link' ) ) {
return;
}
$r = nahnls_send_magic_link( $user_id, home_url( '/getting-started/' ), [ 'force' => true ] );
if ( is_wp_error( $r ) ) {
error_log( 'Invite failed: ' . $r->get_error_code() );
}
} ); Possible error codes: nahnls_disabled, nahnls_unknown_user, nahnls_not_allowed, nahnls_account_rate_limited, nahnls_no_email.
Show the Login & security panel#
Give members a page where they manage their own passkeys, two-factor settings and sign-in options:
echo nahnls_account_security_panel( [ 'ui' => 'plain' ] ); Or use the shortcode on any page:
[nahnls_account_security] ui can be 'plain' (neutral classes) or 'wc' (WooCommerce classes). Visitors who aren’t signed in see a sign-in prompt. To restyle the panel, see Front-end theming.
Read a user’s passkey and 2FA state#
| Function | Returns | Use it to |
|---|---|---|
nahnls_user_passkeys( $user_id ) | array | List a user’s passkeys (public fields only) |
nahnls_user_has_passkey( $user_id ) | bool | Show or hide a “set up a passkey” prompt |
nahnls_delete_passkey( $user_id, $credential_id ) | bool | Remove one passkey |
nahnls_user_can_use_passkeys( $user ) | bool | Check whether passkeys are allowed for the user |
nahnls_user_can_use_magic_link( $user ) | bool | Check whether magic links are allowed for the user |
nahnls_user_2fa_status( $user_id ) | array | Read the user’s 2FA state |
Example: nudge users who only have email codes to add something stronger.
$st = nahnls_user_2fa_status( get_current_user_id() );
if ( $st['enabled'] && [ 'email' ] === $st['methods'] ) {
echo '<a href="/account/security/">Add an authenticator app or passkey</a>';
} Full parameter and return details are in Functions reference.
Recipes#
Passkey and magic link in a page-builder login form
In Elementor, Bricks or Oxygen, drop the two shortcodes into a Shortcode or HTML element inside your login template:
[nahnls_passkey_button redirect="/members/"]
[nahnls_magic_link_form redirect="/members/"] Brand the login-link email
add_filter( 'nahnls_magic_link_email', function ( array $email, WP_User $user, string $url ): array {
$email['subject'] = sprintf( 'Your sign-in link for %s', get_bloginfo( 'name' ) );
$email['message'] = my_theme_email_template( [
'heading' => 'Hi ' . $user->display_name,
'button' => [ 'label' => 'Sign in', 'url' => $url ],
] );
$email['headers'][] = 'Reply-To: support@example.com';
return $email;
}, 10, 3 ); You can also change colours, logo and wording without code. See Email branding in the settings docs.
Only allow passkeys for paying members
add_filter( 'nahnls_passkey_user_allowed', function ( bool $allowed, WP_User $user ): bool {
if ( ! $allowed ) {
return false;
}
return function_exists( 'pmpro_hasMembershipLevel' )
? pmpro_hasMembershipLevel( [ 'pro', 'team' ], $user->ID )
: $allowed;
}, 10, 2 ); Everyone else keeps signing in with a password and their second factor.
Send staff and members to different places, and shorten staff links
add_filter( 'nahnls_passkey_login_redirect', fn( string $url, WP_User $u ) =>
user_can( $u, 'edit_posts' ) ? admin_url() : home_url( '/account/' ), 10, 2 );
add_filter( 'nahnls_magic_link_redirect', fn( string $url, WP_User $u ) =>
home_url( '/account/' ), 10, 2 );
add_filter( 'nahnls_magic_link_ttl', fn( int $s, WP_User $u ) =>
user_can( $u, 'manage_options' ) ? 5 * MINUTE_IN_SECONDS : $s, 10, 2 ); WooCommerce, MemberPress and other login forms
- WooCommerce: with Also show on the WooCommerce My Account login form turned on for passkeys and for email login links (Passwordless tab), the passkey button and “Email me a login link” appear on the My Account login form by themselves, and the Login & security tab is added to My Account. You don’t need code.
- MemberPress, Ultimate Member and similar: place
[nahnls_passkey_button]or[nahnls_magic_link_form]in the plugin’s login template, or call the template tags from the action the plugin fires inside its login form. Give members a page with[nahnls_account_security]. - Use the
nahnls_passkey_user_allowedandnahnls_magic_link_user_allowedfilters to tie access to a membership level.
React to sign-in events
add_action( 'nahnls_passkey_login', function ( WP_User $user, array $credential ) {
// For example, record the sign-in method in your own analytics.
}, 10, 2 );
add_action( 'nahnls_magic_link_login', function ( WP_User $user, string $ip ) {
// A magic link was used.
}, 10, 2 ); All passwordless hooks are listed in Actions and filters.
Things to know#
- Changing the passkey domain breaks existing passkeys. A passkey belongs to one domain (the “relying party ID”). The
nahnls_passkey_rp_idfilter and the passkey domain setting change it. If you change it, users must register new passkeys. See Passkeys. - Role redirects. The
nahnls_passkey_login_redirectandnahnls_magic_link_redirectfilters set the landing page for these sign-ins. The role-based redirects from the Access tab are not applied to them. See Login and logout redirects. - Rate limits. Magic-link requests are limited per IP and per account (3 each per 15 minutes by default). Change them with
nahnls_magic_link_rate_limits. - Headless sites. A front end on another host needs the allowed-origins filter and CORS. See REST API
Was this helpful?
Thanks for your feedback!
✓ Feedback received. Thank you!