N Docs
← Back to site

Functions

3 min readUpdated November 23, 2026

Another plugin on the same WordPress site can make links and clients, listen to every event and change data before it’s saved. Everything below works only while Nahnu Links is active, so check with function_exists() first.

They return a WP_Error instead of throwing, and run as the site: check current_user_can() yourself before acting for a user.

if ( function_exists( 'nahnu_links_ready' ) && nahnu_links_ready() ) {
    $client = nahnu_links_get_client( 'ext:' . $user_id )
        ?: nahnu_links_create_client( [ 'name' => $company, 'email' => $email, 'external_id' => (string) $user_id ] );
    $link = nahnu_links_create_link( [ 'destination_url' => $url, 'slug' => 'spring' ], $client['id'] );
}
FunctionReturns
nahnu_links_ready()Whether Bunny is set up.
nahnu_links_create_link( $args, $client_id = 0 )The link, with short_url. $args takes the same fields as the API.
nahnu_links_get_link( $id )The link, or null.
nahnu_links_get_links( [ client_id, domain_id, search, limit, offset ] )A list of links.
nahnu_links_update_link( $id, $args )The link after the change.
nahnu_links_delete_link( $id )bool
nahnu_links_short_url( $link_or_id )The link’s address.
nahnu_links_get_stats( [ link_id, client_id, domain_id, days ] )Totals, clicks by day, top lists.
nahnu_links_create_client( $args )The client: name, email, plan, external_id, metadata, limits, is_active.
nahnu_links_get_client( $id )The client by id or 'ext:ID', or null.
nahnu_links_get_client_by_external_id( $ext )The client, or null.
nahnu_links_update_client( $id, $args )The client after the change.
nahnu_links_delete_client( $id )bool
nahnu_links_create_api_key( $client_id, [ label, scope, rate_limit ] )[ 'key' => …, 'id' => … ]. The key is shown once.
nahnu_links_revoke_api_key( $key_id )bool
nahnu_links_add_domain( $domain, $client_id = 0 )The domain with its DNS record. It’s added to Bunny.
nahnu_links_get_domain( $id )The domain with its status.
nahnu_links_delete_domain( $id )bool
nahnu_links_add_webhook( [ url, events, client_id, description ] )The webhook, with its secret.
nahnu_links_emit_event( 'custom.name', $data, $client_id )The event’s number. Sent to webhooks and actions like any other event.
nahnu_links_get_events( [ after_id, type, client_id, limit ] )A list of events.
nahnu_links_verify_webhook( $secret, $body, $header )bool: whether an X-NL-Signature header is right and less than 5 minutes old.

Recipes#

Give every new WooCommerce customer a short link to their referral page

add_action( 'woocommerce_created_customer', function ( $user_id ) {
    if ( ! function_exists( 'nahnu_links_ready' ) || ! nahnu_links_ready() ) return;
    $link = nahnu_links_create_link( [
        'destination_url' => home_url( '/?ref=' . $user_id ),
        'title'           => 'Referral, user ' . $user_id,
    ] );
    if ( ! is_wp_error( $link ) ) update_user_meta( $user_id, 'referral_link', $link['short_url'] );
} );

A short link for every new post

add_action( 'publish_post', function ( $post_id, $post ) {
    if ( get_post_meta( $post_id, '_short_link', true ) || ! function_exists( 'nahnu_links_create_link' ) ) return;
    $link = nahnu_links_create_link( [ 'destination_url' => get_permalink( $post_id ), 'slug' => $post->post_name, 'title' => $post->post_title ] );
    if ( ! is_wp_error( $link ) ) update_post_meta( $post_id, '_short_link', $link['short_url'] );
}, 10, 2 );

Show a link’s clicks in a shortcode

add_shortcode( 'link_clicks', function ( $atts ) {
    if ( ! function_exists( 'nahnu_links_get_stats' ) ) return '';
    $s = nahnu_links_get_stats( [ 'link_id' => (int) $atts['id'], 'days' => 30 ] );
    return is_wp_error( $s ) ? '' : number_format_i18n( $s['clicks'] ) . ' clicks this month';
} );

Good to know#

  • Functions talk to your Bunny database, so each call takes a network round trip. Avoid calling them on every front-end page load; cache results with transients.
  • Changes made with these functions go through the same checks, limits and events as the dashboard.