wppaste
WordPress

wp_nonce_field( int|string $action = -1, string $name = '_wpnonce', bool $referer = true, bool $display = true ): string

Since
2.0.4
Source
wp-includes/functions.php:1899

Outputs a hidden `<input>` field containing a nonce generated for the given $action, and appends a referer field unless $referer is set to false. Pass $display as false to get the markup back as a string instead of having it echoed immediately, which is useful when you're assembling a form in a buffer or a REST/AJAX response. Pair it with check_admin_referer() or wp_verify_nonce() on submission, using the same $action and $name values you passed here.

Retrieves or display nonce hidden field for forms.

Description

The nonce field is used to validate that the contents of the form came from the location on the current site and not somewhere else. The nonce does not offer absolute protection, but should protect against most cases. It is very important to use nonce field in forms.

The $action and $name are optional, but if you want to have better security, it is strongly suggested to set those two parameters. It is easier to just call the function without any parameters, because validation of the nonce doesn't require any parameters, but since crackers know what the default is it won't be difficult for them to find a way around your nonce and cause damage.

The input name will be whatever $name value you gave. The input value will be the nonce creation value.

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

$actionint|stringoptional
Action name. Default -1.Default: -1
$namestringoptional
Nonce name. Default '_wpnonce'.Default: '_wpnonce'
$refererbooloptional
Whether to set the referer field for validation. Default true.Default: true
$displaybooloptional
Whether to display or return hidden form field. Default true.Default: true

Return value

string
Nonce field HTML markup.

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.

Add a nonce field to a custom admin form that updates post meta

Render a small form for editing the price meta on post 2, protected by a nonce tied to that specific post and action.

echo '<form method="post" action="">';
wp_nonce_field( 'update_price_2', 'price_nonce' );
echo '<input type="hidden" name="post_id" value="2" />';
echo '<input type="text" name="price" value="' . esc_attr( get_post_meta( 2, 'price', true ) ) . '" />';
echo '</form>';
echo esc_html( 'Nonce field rendered above, scoped to the update_price_2 action.' );

The action string includes the post ID so a nonce copied from one post's edit form cannot be reused on another post's form.

Capture the nonce field markup as a string instead of echoing it

Set $display to false to get the HTML back without it being printed, then inspect or reuse the markup yourself.

$nonce_html = wp_nonce_field( 'export_prices', 'export_nonce', true, false );

echo '<p>Generated markup (not yet output by the function itself):</p>';
echo esc_html( $nonce_html );

With $display false, wp_nonce_field() no longer echoes anything on its own, only the return value contains the markup.

Common problems and fixes · 4

Why does my form end up with the nonce field twice?

By default $display is true, so wp_nonce_field() already echoes the markup while also returning it. Echoing the return value on top of that duplicates the field.

Why does check_admin_referer() say the nonce is invalid even though the field is there?

wp_create_nonce() (used internally) ties the nonce to the exact $action string, and the field's name comes from $name. If the verification call uses a different action or looks for a different field name, it will never match.

Is it safe to just call wp_nonce_field() with no arguments?

It works, since $action defaults to -1 and $name to '_wpnonce', but the long description in core explicitly warns that the default action is predictable, so it offers weaker protection for forms that perform meaningful changes.

Why is there an extra _wp_http_referer field I didn't add?

The $referer parameter defaults to true, which makes the function call wp_referer_field(false) internally and append a hidden referer input alongside the nonce field.

Alternatives and related functions

wp_create_nonce
When you only need the raw nonce value (for example to put in a URL query string or a JS variable) rather than a ready-made hidden form field.
wp_nonce_url
When the nonce needs to be attached to a link's query string instead of a form field.
wp_verify_nonce
When you're on the receiving end of the form and need to check the nonce yourself without the redirect-and-die behavior of check_admin_referer().
check_admin_referer
When you want the nonce and referer created by wp_nonce_field() verified in one call that stops execution automatically on failure.

Performance profile

How much work a call to wp_nonce_field() 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
20–25

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 25.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
42

42 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below wp_nonce_field()

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 · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_nonce_field() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always20–21esc_attr(), wp_create_nonce()
always24–25esc_attr(), wp_create_nonce(), wp_referer_field()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 25 instructions, 20–25 executed per call, 2 branches. The work does not change between versions.

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

Used by · 42

Show all 42

Source code

function wp_nonce_field( $action = -1, $name = '_wpnonce', $referer = true, $display = true ) {	$name        = esc_attr( $name );	$nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . wp_create_nonce( $action ) . '" />'; 	if ( $referer ) {		$nonce_field .= wp_referer_field( false );	} 	if ( $display ) {		echo $nonce_field;	} 	return $nonce_field;}

Changelog

Introduced in 2.0.4. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 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.