Ticket #3628 (closed enhancement: fixed)

Opened 5 years ago

Last modified 4 years ago

Make wp_nonce_field, wp_referrer_field and wp_original_referer_field be able to return output

Reported by: BjornW Owned by: robmil
Priority: normal Milestone: 2.5
Component: General Version: 2.3
Severity: minor Keywords: bg|has-patch
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

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

Change History

BjornW5 years ago

BjornW5 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

  • Severity changed from normal to minor
  • Component changed from Optimization to General
  • 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
  • Milestone changed from 2.1 to 2.2
  • Keywords proposal, wp_nonce_field, wp_referrer_field, wp_original_referer_field removed

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: ↓ 5   BjornW5 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 :)

  • Milestone changed from 2.2 to 2.3

comment:5 in reply to: ↑ 3   foolswisdom5 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 ?

robmil5 years ago

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

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

comment:7   ryan4 years ago

  • Milestone changed from 2.3 to 2.4
  • Status changed from assigned to closed
  • Resolution set to fixed

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