[英]Woocommerce: validate custom fields on my account edit page
I need to validate and save my custom fields in "Edit my account" page.我需要在“编辑我的帐户”页面中验证并保存我的自定义字段。 I found template of this page at
woocommerce/myaccount/form-edit-account.php
.我在
woocommerce/myaccount/form-edit-account.php
找到了这个页面的模板。 I managed to add my custom fields in this template file.我设法在这个模板文件中添加了我的自定义字段。 Now I need to validate them and if it's all good - save user data.
现在我需要验证它们,如果一切顺利 - 保存用户数据。
What hook do I need to use in this situation?在这种情况下我需要使用什么钩子? I can edit
class-wc-form-handler.php
but I don't want to.我可以编辑
class-wc-form-handler.php
但我不想。
Thanks谢谢
I found out an action hook that can be used to validate custom fields or existing fields on Edit Account page.我发现了一个动作挂钩,可用于验证“编辑帐户”页面上的自定义字段或现有字段。
Add following code in functions.php.在functions.php中添加以下代码。
add_action('user_profile_update_errors','wdm_validate_custom_field',10,1);
function wdm_validate_custom_field($args)
{
if ( isset( $_POST['custom_field'] ) )
{
if(strlen($_POST['custom_field'])<6 )
$args->add( 'error', __( 'Your error message', 'woocommerce' ),'');
}
}
可以使用此操作挂钩 woocommerce_save_account_details_errors 在编辑帐户页面验证自定义字段
Try This.尝试这个。
add_filter( 'woocommerce_process_myaccount_field_{your_custom_field_name}' , 'validate_edit_address' );
function validate_edit_address() {
$variable = $_POST['your_custom_field_name'];
if ( preg_match("/[ก-๙a-zA-Z]+$/", $variable ) ){
wc_add_notice( __( '<strong>Error</strong>' ), 'error' );
}
return $variable ;
}
Example :例子 :
add_filter( 'woocommerce_process_myaccount_field_billing_house_number' , 'validate_edit_address' );
function validate_edit_address() {
$variable = $_POST['billing_house_number'];
if ( preg_match("/[ก-๙a-zA-Z]+$/", $variable ) ){
wc_add_notice( __( '<strong>Error</strong>' ), 'error' );
}
return $variable ;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.