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

Retrieve or display nonce hidden field for forms.


Description 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.


Parameters Parameters

$action

(int|string) (Optional) Action name.

Default value: -1

$name

(string) (Optional) Nonce name.

Default value: '_wpnonce'

$referer

(bool) (Optional) Whether to set the referer field for validation.

Default value: true

$echo

(bool) (Optional) Whether to display or return hidden form field.

Default value: true


Top ↑

Return Return

(string) Nonce field HTML markup.


Top ↑

Source Source

File: wp-includes/functions.php

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;
}

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 Codex

    Basic Examples
    While less secure than the examples that follow, this is the simplest implementation which omits all arguments. In your form add the following:

    <?php wp_nonce_field(); ?>
    

    It’s better to name your action and nonce in your form. Enter values for the first and second arguments to print the necessary hidden field:

    <form method="post">
       <!-- some inputs here ... -->
       <?php wp_nonce_field( 'name_of_my_action', 'name_of_nonce_field' ); ?>
    </form>
    

    Then in the page where it is being submitted to, you may verify it using the wp_verify_nonce() function. Notice that you have to manually retrieve the nonce (from the $_POST array in this example), and the name of the action is the 2nd parameter instead of the first:

    if ( ! isset( $_POST['name_of_nonce_field'] ) 
        || ! wp_verify_nonce( $_POST['name_of_nonce_field'], 'name_of_my_action' ) 
    ) {
       print 'Sorry, your nonce did not verify.';
       exit;
    } else {
       // process form data
    }
    

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