wp_nonce_field() – Retrieve or display nonce hidden field for forms.

You appear to be a bot. Output may be restricted

Description

Retrieve or display nonce hidden field for forms.

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.

Usage

$string = wp_nonce_field( $action, $name, $referer, $echo );

Parameters

$action
( int|string ) optional default: -1 – Optional. Action name. Default -1.
$name
( string ) optional default: _wpnonce – Optional. Nonce name. Default '_wpnonce'.
$referer
( bool ) optional default: 1 – Optional. Whether to set the referer field for validation. Default true.
$echo
( bool ) optional default: 1 – Optional. Whether to display or return hidden form field. Default true.

Returns

string Nonce field HTML markup.

Source

File name: wordpress/wp-includes/functions.php
Lines:

1 to 14 of 14
function wp_nonce_field( $action = -1, $name = '_wpnonce', $referer = true, $echo = 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 ( $echo ) {
    echo $nonce_field;
  }

  return $nonce_field;
}
 

 View on GitHub View on Trac