我正在尝试创建WooCommerce的注册表单。问题是表单在没有wp\\u nonce的情况下无法工作,所以我尝试将其包括在内。
我在中创建了一个短代码functions.php
显示表单,但当我使用wp_nonce_field()
, 表单中断。
以下是按预期工作的代码(wp_nonce_field()
已注释掉),但由于没有wp\\u nonce,表单不会发布:
add_shortcode(\'signup_form\', \'get_signup_form\');
function get_signup_form() {
//$wp_nonce_code = wp_nonce_field();
$signup_form_code = <<<EOT
<form method="post" class="popup-register" action="/my-account/">
<p style="text-align:center;"><input type="email" class="popup-input" style="width: 65%;" name="email" id="reg_email" placeholder="Enter your email address"><input type="submit" class="popup-input-submit button" style="margin: 0; border: 1px solid #999999;line-height: 19px;" name="register" value="Register"></p>
{$wp_nonce_code}
</form>
EOT;
return $signup_form_code;
}
当我取消注释时
wp_nonce_field()
为了尝试使表单工作,表单根本不显示-只是空白。E、 g.:
add_shortcode(\'signup_form\', \'get_signup_form\');
function get_signup_form() {
$wp_nonce_code = wp_nonce_field();
$signup_form_code = <<<EOT
<form method="post" class="popup-register" action="/my-account/">
<p style="text-align:center;"><input type="email" class="popup-input" style="width: 65%;" name="email" id="reg_email" placeholder="Enter your email address"><input type="submit" class="popup-input-submit button" style="margin: 0; border: 1px solid #999999;line-height: 19px;" name="register" value="Register"></p>
{$wp_nonce_code}
</form>
EOT;
return $signup_form_code;
}
我看不出有什么问题
wp_nonce_field()
而这些文件并没有真正起到帮助作用。
最合适的回答,由SO网友:Dave Romsey 整理而成
wp_nonce_field()
默认情况下将回显其输出,因此设置$echo
参数到false
:
add_shortcode(\'signup_form\', \'get_signup_form\');
function get_signup_form() {
$wp_nonce_code = wp_nonce_field( -1, \'_wpnonce\', true, false );
$signup_form_code = <<<EOT
<form method="post" class="popup-register" action="/my-account/">
<p style="text-align:center;">
<input type="email" class="popup-input" style="width: 65%;" name="email" id="reg_email" placeholder="Enter your email address">
<input type="submit" class="popup-input-submit button" style="margin: 0; border: 1px solid #999999;line-height: 19px;" name="register" value="Register">
</p>
{$wp_nonce_code}
</form>
EOT;
return $signup_form_code;
}