bb_nonce_field

Retrieve or display nonce hidden field for forms.

Description

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

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.

If you set $echo to true and set $referer to true, then you will need to retrieve the . If you have the $referer set to true and are echoing the nonce field, it will also echo the referer field.

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.

Returns (string)

Nonce field.

Parameters (4)

0. $action — Optional. (int) => -1
Action name.
1. $name — Optional. (string) => '_wpnonce'
Nonce name.
2. $referer — Optional. (bool) => true
Optional, default true. Whether to set the referer field for validation.
3. $echo — Optional. (bool) => true
Optional, default true. Whether to display or return hidden form field.

Usage

  1. if ( !function_exists( 'bb_nonce_field' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-core.php'; 
  3.  
  4. // Optional. Action name. 
  5. $action = -1; 
  6.  
  7. // Optional. Nonce name. 
  8. $name = '_wpnonce'; 
  9.  
  10. // Optional, default true. Whether to set the referer field for validation. 
  11. $referer = true; 
  12.  
  13. // Optional, default true. Whether to display or return hidden form field. 
  14. $echo = true; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = bb_nonce_field($action, $name, $referer, $echo); 
  18.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/bbpress/bb-includes/functions.bb-core.php  
  1. function bb_nonce_field( $action = -1, $name = "_wpnonce", $referer = true , $echo = true ) { 
  2. $name = esc_attr( $name ); 
  3. $nonce = bb_create_nonce( $action ); 
  4. $nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . $nonce . '" />'; 
  5. if ( $echo ) 
  6. echo $nonce_field; 
  7.  
  8. if ( $referer ) 
  9. wp_referer_field( $echo, 'previous' ); 
  10.  
  11. return $nonce_field;