WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 6 years ago

#3628 closed enhancement (fixed)

Make wp_nonce_field, wp_referrer_field and wp_original_referer_field be able to return output

Reported by: BjornW Owned by: robmil
Milestone: 2.5 Priority: normal
Severity: minor Version: 2.3
Component: General Keywords: bg|has-patch
Focuses: Cc:

Description

The wp_nonce_field, wp_referrer_field and wp_original_referer_field functions should return values instead of using echo. This would be more consistent with for instance wp_nonce_url which does return value. My proposal is easy to implement because the default behaviour is still using echo. If you want to get the value returned one can add the $return = TRUE parameter and the value will be returned instead of echo'ed.


Why is this useful?

  1. consistency
  2. useful for building forms using vars instead of mingling PHP and HTML e.g:
function formbuilder($formdata = NULL) {
  if ( function_exists('wp_nonce_field') ) { 
     $nonce_field = wp_nonce_field('faces-update-face_upload', $return = TRUE);
  }

  $form   = '<form>';
  $form  .= $nonce_field; 
  $form  .= '<input type="text" />';
  $form  .= '</form>'

  return $form
}

Basically it enhances the way these functions can be used and therefor also expands the possible ways of using the code, while maintaining the old behavior. Somewhere in the future the echo functionality could be deprecated and functions would be consistent in returning values instead of echo'ing.

Attachments (3)

wp_nonces.diff (8.6 KB) - added by BjornW 7 years ago.
wp_nonces.2.diff (1.9 KB) - added by BjornW 7 years ago.
3628.diff (1.5 KB) - added by robmil 7 years ago.
This is probably a little more in keeping with the rest of our idiosyncrasies, plus the same treatment for wp_referer_field

Download all attachments as: .zip

Change History (11)

BjornW7 years ago

BjornW7 years ago

comment:1 BjornW7 years ago

Please ignore the first attached diff. It contains too much info which is not relevant for this ticket. Sorry, I'm kinda new to Trac and didn't know how to remove the false one. All the info you need is in wp_nonces.2.diff

comment:2 Viper007Bond7 years ago

  • Component changed from Optimization to General
  • Keywords proposal wp_nonce_field wp_referrer_field wp_original_referer_field removed
  • Milestone changed from 2.1 to 2.2
  • Severity changed from normal to minor
  • Summary changed from Proposal: wp_nonce_field, wp_referrer_field and wp_original_referer_field should return value to Make wp_nonce_field, wp_referrer_field and wp_original_referer_field be able to return output
  • Version set to 2.0.7

Patch needs some cleaning up to better match our coding standards as well as making use of some of our custom functions (I believe we have a echo vs. return function), but as to the actual idea itself, I like it. Very useful for those times when you are generating output or whatnot and don't want to echo at that moment.

comment:3 follow-up: BjornW7 years ago

Viper007Bond,

Could you please tell me what I need to change in my coding style to better match with the preferred style? If you do find the custom function let me know where I can find it so I can change this as well.

Good to hear my proposal is useful :)

comment:4 foolswisdom7 years ago

  • Milestone changed from 2.2 to 2.3

comment:5 in reply to: ↑ 3 foolswisdom7 years ago

Replying to BjornW:

Could you please tell me what I need to change in my coding style to better match with
the preferred style?

http://codex.wordpress.org/WordPress_Coding_Standards ?

robmil7 years ago

This is probably a little more in keeping with the rest of our idiosyncrasies, plus the same treatment for wp_referer_field

comment:6 robmil7 years ago

  • Keywords bg|has-patch added
  • Owner changed from anonymous to robmil
  • Status changed from new to assigned
  • Version changed from 2.0.7 to 2.3

comment:7 ryan7 years ago

  • Milestone changed from 2.3 to 2.4

comment:8 westi6 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [7323]) Allow for wp_nonce_field, wp_referrer_field and wp_original_referer_field be able to return output. Fixes #3628 props robmil.

Note: See TracTickets for help on using tickets.