Title: wp_nonce_field
Published: April 25, 2014
Last modified: August 20, 2026

---

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

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#user-contributed-notes)

Retrieves or display nonce hidden field for forms.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#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](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#parameters)󠁿

 `$action`int|stringoptional

Action name.

Default:`-1`

`$name`stringoptional

Nonce name. Default `'_wpnonce'`.

Default:`'_wpnonce'`

`$referer`booloptional

Whether to set the referer field for validation.

Default:`true`

`$display`booloptional

Whether to display or return hidden form field.

Default:`true`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#return)󠁿

 string Nonce field HTML markup.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#source)󠁿

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/functions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/functions.php#L1906)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/functions.php#L1906-L1919)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_referer_field()](https://developer.wordpress.org/reference/functions/wp_referer_field/)`wp-includes/functions.php` |

Retrieves or displays referer hidden field for forms.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  | 
| [wp_create_nonce()](https://developer.wordpress.org/reference/functions/wp_create_nonce/)`wp-includes/pluggable.php` |

Creates a cryptographic token tied to a specific action, user, user session, and window of time.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#)

| Used by | Description | 
| [the_block_editor_meta_boxes()](https://developer.wordpress.org/reference/functions/the_block_editor_meta_boxes/)`wp-admin/includes/post.php` |

Renders the meta boxes forms.

  | 
| [the_block_editor_meta_box_post_form_hidden_fields()](https://developer.wordpress.org/reference/functions/the_block_editor_meta_box_post_form_hidden_fields/)`wp-admin/includes/post.php` |

Renders the hidden form required for the meta boxes form.

  | 
| [network_step1()](https://developer.wordpress.org/reference/functions/network_step1/)`wp-admin/includes/network.php` |

Prints step 1 for Network installation process.

  | 
| [WP_Screen::render_screen_options()](https://developer.wordpress.org/reference/classes/wp_screen/render_screen_options/)`wp-admin/includes/class-wp-screen.php` |

Renders the screen options tab.

  | 
| [install_themes_upload()](https://developer.wordpress.org/reference/functions/install_themes_upload/)`wp-admin/includes/theme-install.php` |

Displays a form to upload themes from zip files.

  | 
| [WP_List_Table::display_tablenav()](https://developer.wordpress.org/reference/classes/wp_list_table/display_tablenav/)`wp-admin/includes/class-wp-list-table.php` |

Generates the table navigation above or below the table.

  | 
| [admin_color_scheme_picker()](https://developer.wordpress.org/reference/functions/admin_color_scheme_picker/)`wp-admin/includes/misc.php` |

Displays the default administration color scheme picker (Used in user-edit.php).

  | 
| [WP_Theme_Install_List_Table::display()](https://developer.wordpress.org/reference/classes/wp_theme_install_list_table/display/)`wp-admin/includes/class-wp-theme-install-list-table.php` |

Displays the theme install table.

  | 
| [install_plugins_upload()](https://developer.wordpress.org/reference/functions/install_plugins_upload/)`wp-admin/includes/plugin-install.php` |

Displays a form to upload plugins from zip files.

  | 
| [_wp_dashboard_control_callback()](https://developer.wordpress.org/reference/functions/_wp_dashboard_control_callback/)`wp-admin/includes/dashboard.php` |

Outputs controls for the current dashboard widget.

  | 
| [wp_dashboard()](https://developer.wordpress.org/reference/functions/wp_dashboard/)`wp-admin/includes/dashboard.php` |

Displays the dashboard.

  | 
| [wp_dashboard_quick_press()](https://developer.wordpress.org/reference/functions/wp_dashboard_quick_press/)`wp-admin/includes/dashboard.php` |

Displays the Quick Draft widget.

  | 
| [settings_fields()](https://developer.wordpress.org/reference/functions/settings_fields/)`wp-admin/includes/plugin.php` |

Outputs nonce, action, and option_page fields for a settings page.

  | 
| [find_posts_div()](https://developer.wordpress.org/reference/functions/find_posts_div/)`wp-admin/includes/template.php` |

Outputs the modal window used for attaching media to posts or pages in the media-listing screen.

  | 
| [wp_comment_reply()](https://developer.wordpress.org/reference/functions/wp_comment_reply/)`wp-admin/includes/template.php` |

Outputs the in-line comment reply-to form in the Comments list table.

  | 
| [_list_meta_row()](https://developer.wordpress.org/reference/functions/_list_meta_row/)`wp-admin/includes/template.php` |

Outputs a single row of public meta data in the Custom Fields meta box.

  | 
| [meta_form()](https://developer.wordpress.org/reference/functions/meta_form/)`wp-admin/includes/template.php` |

Prints the form in the Custom Fields meta box.

  | 
| [WP_Themes_List_Table::display()](https://developer.wordpress.org/reference/classes/wp_themes_list_table/display/)`wp-admin/includes/class-wp-themes-list-table.php` |

Displays the themes table.

  | 
| [media_upload_type_form()](https://developer.wordpress.org/reference/functions/media_upload_type_form/)`wp-admin/includes/media.php` |

Outputs the legacy media upload form for a given media type.

  | 
| [media_upload_type_url_form()](https://developer.wordpress.org/reference/functions/media_upload_type_url_form/)`wp-admin/includes/media.php` |

Outputs the legacy media upload form for external media.

  | 
| [media_upload_gallery_form()](https://developer.wordpress.org/reference/functions/media_upload_gallery_form/)`wp-admin/includes/media.php` |

Adds gallery form to upload iframe.

  | 
| [media_upload_library_form()](https://developer.wordpress.org/reference/functions/media_upload_library_form/)`wp-admin/includes/media.php` |

Outputs the legacy media upload form for the media library.

  | 
| [post_comment_meta_box()](https://developer.wordpress.org/reference/functions/post_comment_meta_box/)`wp-admin/includes/meta-boxes.php` |

Displays comments for post.

  | 
| [link_categories_meta_box()](https://developer.wordpress.org/reference/functions/link_categories_meta_box/)`wp-admin/includes/meta-boxes.php` |

Displays link categories form fields.

  | 
| [post_categories_meta_box()](https://developer.wordpress.org/reference/functions/post_categories_meta_box/)`wp-admin/includes/meta-boxes.php` |

Displays post categories form fields.

  | 
| [WP_Post_Comments_List_Table::display()](https://developer.wordpress.org/reference/classes/wp_post_comments_list_table/display/)`wp-admin/includes/class-wp-post-comments-list-table.php` |  | 
| [WP_Comments_List_Table::extra_tablenav()](https://developer.wordpress.org/reference/classes/wp_comments_list_table/extra_tablenav/)`wp-admin/includes/class-wp-comments-list-table.php` |

Displays extra controls between bulk actions and pagination.

  | 
| [WP_Comments_List_Table::display()](https://developer.wordpress.org/reference/classes/wp_comments_list_table/display/)`wp-admin/includes/class-wp-comments-list-table.php` |

Displays the comments table.

  | 
| [WP_Terms_List_Table::inline_edit()](https://developer.wordpress.org/reference/classes/wp_terms_list_table/inline_edit/)`wp-admin/includes/class-wp-terms-list-table.php` |

Outputs the hidden row displayed when inline editing

  | 
| [request_filesystem_credentials()](https://developer.wordpress.org/reference/functions/request_filesystem_credentials/)`wp-admin/includes/file.php` |

Displays a form to the user to request for their FTP/SSH details in order to connect to the filesystem.

  | 
| [WP_Posts_List_Table::inline_edit()](https://developer.wordpress.org/reference/classes/wp_posts_list_table/inline_edit/)`wp-admin/includes/class-wp-posts-list-table.php` |

Outputs the hidden row displayed when inline editing

  | 
| [Custom_Image_Header::step_1()](https://developer.wordpress.org/reference/classes/custom_image_header/step_1/)`wp-admin/includes/class-custom-image-header.php` |

Displays first step of custom header image page.

  | 
| [Custom_Image_Header::step_2()](https://developer.wordpress.org/reference/classes/custom_image_header/step_2/)`wp-admin/includes/class-custom-image-header.php` |

Displays second step of custom header image page.

  | 
| [confirm_delete_users()](https://developer.wordpress.org/reference/functions/confirm_delete_users/)`wp-admin/includes/ms.php` |  | 
| [list_core_update()](https://developer.wordpress.org/reference/functions/list_core_update/)`wp-admin/update-core.php` |

Lists available core updates.

  | 
| [list_plugin_updates()](https://developer.wordpress.org/reference/functions/list_plugin_updates/)`wp-admin/update-core.php` |

Display the upgrade plugins form.

  | 
| [list_theme_updates()](https://developer.wordpress.org/reference/functions/list_theme_updates/)`wp-admin/update-core.php` |

Display the upgrade themes form.

  | 
| [list_translation_updates()](https://developer.wordpress.org/reference/functions/list_translation_updates/)`wp-admin/update-core.php` |

Display the update translations form.

  | 
| [Custom_Background::admin_page()](https://developer.wordpress.org/reference/classes/custom_background/admin_page/)`wp-admin/includes/class-custom-background.php` |

Displays the custom background page.

  | 
| [signup_nonce_fields()](https://developer.wordpress.org/reference/functions/signup_nonce_fields/)`wp-includes/ms-functions.php` |

Adds a nonce field to the signup page.

  | 
| [wp_comment_form_unfiltered_html_nonce()](https://developer.wordpress.org/reference/functions/wp_comment_form_unfiltered_html_nonce/)`wp-includes/comment-template.php` |

Displays form token for unfiltered comments.

  | 
| [_WP_Editors::wp_link_dialog()](https://developer.wordpress.org/reference/classes/_wp_editors/wp_link_dialog/)`wp-includes/class-wp-editor.php` |

Dialog for internal linking.

  |

[Show 37 more](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.0.4](https://developer.wordpress.org/reference/since/2.0.4/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#comment-content-801)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/wp_nonce_field/#comment-801)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_nonce_field%2F%23comment-801)
     Vote results for this note: 11[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_nonce_field%2F%23comment-801)
 4.  **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:
 5.      ```php
         <?php wp_nonce_field(); ?>
         ```
     
 6.  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:
 7.      ```php
         <form method="post">
            <!-- some inputs here ... -->
            <?php wp_nonce_field( 'name_of_my_action', 'name_of_nonce_field' ); ?>
         </form>
         ```
     
 8.  Then in the page where it is being submitted to, you may verify it using the [wp_verify_nonce()](https://developer.wordpress.org/reference/functions/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:
 9.      ```php
         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
         }
         ```
     
 10.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_nonce_field%2F%3Freplytocom%3D801%23feedback-editor-801)
 11.  [Skip to note 4 content](https://developer.wordpress.org/reference/functions/wp_nonce_field/?output_format=md#comment-content-6449)
 12.   [Sarathlal N](https://profiles.wordpress.org/sarathlal/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/wp_nonce_field/#comment-6449)
 13. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_nonce_field%2F%23comment-6449)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_nonce_field%2F%23comment-6449)
 14. We can create a nonce field without a name because it is optional. In that case,
     the name will be `_wpnonce`.
 15.     ```php
         wp_nonce_field( 'my-action-name' );
         ```
     
 16. The above statement echo a hidden field named as `_wpnonce`.
 17. To verify this nonce, we can use the `wp_verify_nonce` function.
 18.     ```php
         if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'my-action-name' ) ) {
           //do you action
         } else {
           die( __( 'Security check', 'textdomain' ) );
         }
         ```
     
 19.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_nonce_field%2F%3Freplytocom%3D6449%23feedback-editor-6449)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_nonce_field%2F)
before being able to contribute a note or feedback.