wp_nonce_url( string $actionurl, int|string $action = -1, string $name = '_wpnonce' ): string
- Since
- 2.0.4
- Source
wp-includes/functions.php:1868
Appends a nonce query parameter to a URL so a subsequent request to that link can be verified before running a sensitive action. The action name you pass must match the one used later in wp_verify_nonce() or check_admin_referer(), and the returned string is already run through esc_html(), not esc_url(), so avoid escaping it a second time.
Compatibility
- WordPress
- since 2.0.4
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$actionurlstring- URL to add nonce action.
$actionint|stringoptional- Nonce action name. Default -1.Default:
-1 $namestringoptional- Nonce name. Default '_wpnonce'.Default:
'_wpnonce'
Return value
string- Escaped URL with nonce action added.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Build a nonce-protected trash link for a post
Generate the same kind of nonce-carrying admin link WordPress uses for row actions like Trash.
$post_id = 2;
$trash_url = admin_url( 'post.php?post=' . $post_id . '&action=trash' );
$nonce_url = wp_nonce_url( $trash_url, 'trash-post_' . $post_id );
printf( '<a href="%s">Trash post %d</a>', $nonce_url, $post_id );
echo '<pre>' . esc_html( $nonce_url ) . '</pre>';wp_nonce_url() already escapes its return value, so wrapping the whole string in esc_url() or esc_html() again is unnecessary and can double-encode it.
Give an admin-post.php action link a custom nonce field name
Some handlers check a nonce stored under a name other than the default _wpnonce, which the third argument controls.
$post_id = 2;
$action_url = admin_url( 'admin-post.php?action=clear_price_meta&post_id=' . $post_id );
$nonce_url = wp_nonce_url( $action_url, 'clear_price_meta_' . $post_id, 'price_nonce' );
echo esc_html( $nonce_url );
echo '<br>Current price meta: ' . esc_html( get_post_meta( $post_id, 'price', true ) );The handler for admin-post.php would then read the nonce with $_REQUEST['price_nonce'] instead of $_REQUEST['_wpnonce'].
Common problems and fixes · 3
- Why does check_admin_referer() reject a URL built with wp_nonce_url()?
- Why do the ampersands in my URL look wrong after calling this function?
- Can I use wp_nonce_url() to protect an AJAX or REST request?
Why does check_admin_referer() reject a URL built with wp_nonce_url()?
Why do the ampersands in my URL look wrong after calling this function?
Can I use wp_nonce_url() to protect an AJAX or REST request?
Alternatives and related functions
wp_create_nonce- When you only need the raw nonce string, for example to embed it in JavaScript or a REST header instead of a URL query argument.
wp_nonce_field- When the action is triggered by a form submission rather than a plain link, so the nonce needs to be a hidden input.
check_admin_referer- When you need to verify, on the receiving admin page, the nonce that a wp_nonce_url() link supplied.
wp_verify_nonce- When you want to validate a nonce value manually outside the admin-referer helper, for example inside a custom handler.
Performance profile
How much work a call to wp_nonce_url() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 18
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 18.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_nonce_url()
Further down the call graph this can also reach option, cache, serialize, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_nonce_url() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 18 | wp_create_nonce(), add_query_arg(), esc_html() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 18 | 18 | 0 | |
| 8.5 | 18 | 18 | 0 | |
| 8.4 | 18 | 18 | 0 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 21 | 21 | 0 | |
| 8.2 | 21 | 21 | 0 | |
| 8.1 | 21 | 21 | 0 | |
| 7.4 | 21 | 21 | 0 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- esc_html()Escaping for HTML blocks.
- add_query_arg()Retrieves a modified URL query string.
- wp_create_nonce()Creates a cryptographic token tied to a specific action, user, user session, and window of time.
Used by · 50
- Plugin_Installer_Skin::after()Performs an action following a plugin install.
- Plugin_Installer_Skin::do_overwrite()Checks if the plugin can be overwritten and outputs the HTML for overwriting a plugin on upload.
- Plugin_Upgrader_Skin::after()Performs an action following a single plugin update.
- Theme_Installer_Skin::after()Performs an action following a single theme install.
- Theme_Installer_Skin::do_overwrite()Checks if the theme can be overwritten and outputs the HTML for overwriting a theme on upload.
- Theme_Upgrader_Skin::after()Performs an action following a single theme update.
- WP_Links_List_Table::handle_row_actions()Generates and displays row action links.
- WP_MS_Sites_List_Table::handle_row_actions()Generates and displays row action links.
- WP_MS_Themes_List_Table::column_autoupdates()Handles the auto-updates column output.
- WP_MS_Themes_List_Table::column_name()Handles the name column output.
- WP_MS_Users_List_Table::handle_row_actions()Generates and displays row action links.
- WP_Media_List_Table::_get_row_actions()Gets the row actions for a media item.
Show all 50
- WP_Plugins_List_Table::single_row()
- WP_Posts_List_Table::handle_row_actions()Generates and displays row action links.
- WP_Privacy_Data_Export_Requests_List_Table::column_email()Actions column.
- WP_Privacy_Data_Export_Requests_List_Table::column_next_steps()Displays the next steps column.
- WP_Privacy_Data_Removal_Requests_List_Table::column_email()Outputs the Actions column.
- WP_Privacy_Data_Removal_Requests_List_Table::column_next_steps()Outputs the Next steps column.
- WP_Site_Health::get_test_https_status()Tests if the site is serving content over HTTPS.
- WP_Terms_List_Table::handle_row_actions()Generates and displays row action links.
- WP_Theme_Install_List_Table::install_theme_info()Prints the info for a theme (to be used in the theme installer modal).
- WP_Theme_Install_List_Table::single_row()Prints a theme from the WordPress.org API.
- WP_Themes_List_Table::display_rows()Generates the list table rows.
- WP_Upgrader_Skin::request_filesystem_credentials()Displays a form to the user to request for their FTP/SSH details in order to connect to the filesystem.
- WP_Users_List_Table::single_row()Generates HTML for a single row on the users.php admin panel.
- Walker_Nav_Menu_Edit::start_el()Start the element output.
- _admin_notice_post_locked()Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
- core_auto_updates_settings()Display WordPress auto-updates settings.
- delete_plugins()Removes directory and files of a plugin for a list of plugins.
- delete_theme()Removes a theme.
- do_block_editor_incompatible_meta_box()Renders a "fake" meta box with an information message, shown on the block editor, when an incompatible meta box is found.
- do_core_upgrade()Upgrades WordPress core display.
- do_dismiss_core_update()Dismiss a core update.
- do_undismiss_core_update()Undismiss a core update.
- get_delete_post_link()Retrieves the delete posts link for post.
- get_media_item()Retrieves HTML form for modifying the image attachment.
- get_theme_update_available()Retrieves the update link if there is a theme update available.
- install_plugin_install_status()Determines the status we can perform on a plugin.
- link_submit_meta_box()Displays link create form fields.
- wp_admin_bar_recovery_mode_menu()Adds a link to exit recovery mode when Recovery Mode is active.
- wp_ajax_delete_plugin()Handles deleting a plugin via AJAX.
- wp_ajax_delete_theme()Handles deleting a theme via AJAX.
- wp_dashboard_plugins_output()Display plugins text for the WordPress news widget.
- wp_import_upload_form()Outputs the form used by the importers to accept the data to be imported.
- wp_link_manager_disabled_message()Outputs the 'disabled' message for the WordPress Link Manager.
- wp_load_press_this()
- wp_logout_url()Retrieves the logout URL.
- wp_plugin_update_row()Displays update information for a plugin.
- wp_prepare_revisions_for_js()Prepare revisions for JavaScript.
- wp_prepare_themes_for_js()Prepares themes for JavaScript.
Source code
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 ) );}Changelog
Introduced in 2.0.4. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.