WordPress.org

Make WordPress Core

Opened 6 weeks ago

Last modified 6 weeks ago

#51301 new enhancement

Add class field to wp_nonce_field

Reported by: dingo_d Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch 2nd-opinion
Focuses: Cc:

Description

It would be great to have an optional $class field when generating nonce field. That way, when I'm referencing the field in my JS scripts, I can add a class that specifies that this field will be used in JS.

For instance, every element that is used in JS script has a js- prefix. It's a common practice in JS development.

So I'd have

<?php
wp_nonce_field('action', 'name', true, true, 'js-nonce-field');

The class field would be optional, and by default empty. If it would be empty it wouldn't be displayed.

Change History (4)

This ticket was mentioned in PR #530 on WordPress/wordpress-develop by dingo-d.


6 weeks ago

Added a class field to the nonce field function. Won't be shown if empty.

Trac ticket: https://core.trac.wordpress.org/ticket/51301

#2 @prbot
6 weeks ago

joyously commented on PR #530:

This makes no sense.
Why not simply use wp_create_nonce instead?

#3 @dd32
6 weeks ago

Given the id that should be unique per page, and/or the fact that this should only be used within a form element I'm not sure an additional class here is actually much of a benefit given it's a non-displayed element.

I suspect, that the use-case here is probably one that would be better done through using either a data-nonce=".. wp_create_nonce(...) .." attribute or a wp_localize_script( 'my-script', 'myScriptData', array( 'nonce' => wp_create_nonce(...) );

#4 @dingo_d
6 weeks ago

Hmmm, didn't really think of the localize script version. The nonce field includes the referer field, which is not present in the wp_create_nonce().

Note: See TracTickets for help on using tickets.