Opened 4 years ago
Last modified 4 years 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.
4 years ago
#1
#3
@
4 years 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(...) );
Added a class field to the nonce field function. Won't be shown if empty.
Trac ticket: https://core.trac.wordpress.org/ticket/51301