wp_nonce_url( string $actionurl, int|string $action = -1, string $name = '_wpnonce' )

Retrieve URL with nonce added to URL query.


Description Description


Parameters Parameters

$actionurl

(string) (Required) URL to add nonce action.

$action

(int|string) (Optional) Nonce action name.

Default value: -1

$name

(string) (Optional) Nonce name.

Default value: '_wpnonce'


Top ↑

Return Return

(string) Escaped URL with nonce action added.


Top ↑

Source Source

File: wp-includes/functions.php

function wp_nonce_url( $actionurl, $action = -1, $name = '_wpnonce' ) {
	$actionurl = str_replace( '&', '&', $actionurl );
	return esc_html( add_query_arg( $name, wp_create_nonce( $action ), $actionurl ) );
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.4 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Benjamin Intal

    Note that wp_nonce_url escapes & to & and may cause links or redirects to become incorrect.

    // Sample URL, note the & in there
    $url = 'http://localhost/?arg1=value1&arg2=value2';
    
    // This will show http://localhost/?arg1=value1&arg2=value2&_wpnonce=abcdef
    echo wp_nonce_url( $url, 'action' );
    
    // This will return http://localhost/?arg1=value1&arg2=value2&_wpnonce=abcdef
    echo add_query_arg( '_wpnonce', wp_create_nonce( 'action' ), $url );
    
  2. Skip to note 2 content
    Contributed by Codex

    Example

    Plugin authors can safely add links that perform tasks using a combination of wp_nonce_url() and admin_url().
    For instance, start by creating the link users can click to do something interesting:

    function my_plugin_do_something () {
    ?>
    <h2><?php esc_html_e('My Plugin Admin Screen', 'my-plugin-textdomain');?></h2>
    <p>
        <a href="<?php print wp_nonce_url(admin_url('options.php?page=my_plugin_settings'), 'doing_something', 'my_nonce');?>"
            class="button button-primary"><?php esc_html_e('Do Something!', 'my-plugin-textdomain');?></a>
        <span class="description"><?php esc_html_e('This button does something interesting.', 'my-plugin-textdomain');?></span>
    </p>
    <?php
    }
    

    Then, to detect when the user clicks the link, check the nonce validity using wp_verify_nonce() in the function you defined when you called add_menu_page() or one of its Administration Menus wrappers. If the nonce isn’t valid, the link wasn’t clicked, so display the link. Otherwise, do “something interesting.”

    add_action('admin_menu', 'add_my_plugin_admin_screen');
    function add_my_plugin_admin_screen () {
        add_options_page(
            __('My Plugin Settings', 'my-plugin-textdomain'),
            __('My Plugin', 'my-plugin-textdomain'),
            'manage_options',
            'my_plugin_settings',
            'my_plugin_do_something'
        );
    }
    
    function my_plugin_do_something () {
        if (!isset($_GET['my_nonce']) || !wp_verify_nonce($_GET['my_nonce'], 'doing_something')) {
    ?>
    <h2><?php esc_html_e('My Plugin Admin Screen', 'my-plugin-textdomain');?></h2>
    <p>
        <a href="<?php print wp_nonce_url(admin_url('options.php?page=my_plugin_settings'), 'doing_something', 'my_nonce');?>"
            class="button button-primary"><?php esc_html_e('Do Something!', 'my-plugin-textdomain');?></a>
        <span class="description"><?php esc_html_e('This button does something interesting.', 'my-plugin-textdomain');?></span>
    </p>
    <?php
        } else {
            // User pressed "Do Something!" button, so
            // do something interesting.
        }
    }
    

    Note that the recommended “context” parameter of the nonce is used to disambiguate which button was pressed. If you make more than one button users can press, make sure each button has a different nonce name and/or context.

You must log in before being able to contribute a note or feedback.