第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將過濾后的計(jì)費(fèi)電話保存到 WooCommerce 中的管理員用戶配置文件

將過濾后的計(jì)費(fèi)電話保存到 WooCommerce 中的管理員用戶配置文件

PHP
呼喚遠(yuǎn)方 2023-07-21 15:50:41
我在過去幾天讀到的所有內(nèi)容都表明,如果我想在 user-edit.php (用戶管理后端)中保存字段,我應(yīng)該使用 & 鉤子(并且我在這個(gè)問題中不包括驗(yàn)證鉤子edit_user_profile_update...... personal_options_update)在法典中,他們指出:考慮一個(gè)例子:update_user_meta($user_id, 'custom_meta_key', $_POST['custom_meta_key']);$_POST請(qǐng)務(wù)必確保為數(shù)據(jù)密鑰和實(shí)際用戶元密鑰指定不同的密鑰名稱。如果您對(duì)兩者使用相同的鍵,Wordpress 出于某種原因會(huì)清空該鍵下發(fā)布的值。因此,您總是會(huì)得到一個(gè)空值,$_POST['custom_meta_key']因此請(qǐng)?jiān)?html 輸入元素的 name 屬性中更改它并附加后綴。將其更改為$_POST['custom_meta_key_data'],它將正確傳遞數(shù)據(jù)。但是,考慮到我想向現(xiàn)有billing_phone字段添加驗(yàn)證,我不知道如何創(chuàng)建所述“ custom_meta_key_data”(例如:'_billing_phone'、 或'prefix_billing_phone'),然后將值輸入到所述 prefix_billing_phone 中,然后轉(zhuǎn)換為'billing_phone'via update_user_meta()。我在下面提供了我的基本代碼,我已經(jīng)在互聯(lián)網(wǎng)上搜索了兩天的解決方案,但我找不到解決方法。此外,str_replace()不執(zhí)行操作,這讓我查看內(nèi)部,user-edit.php并且注釋支持上面引用的注釋,在點(diǎn)擊更新和配置文件重新加載之間,它將每個(gè)變量保存為_billing_(前綴“_”)并記錄原始值(前面的 if 語句/下面的代碼)并保存該值 - 不是我條件/嘗試驗(yàn)證的內(nèi)容。我不知道如何復(fù)制它,以便我可以簡單地驗(yàn)證我的字段......add_action( 'personal_options_update', 'audp_save_user_account_fields' );add_action( 'edit_user_profile_update', 'audp_save_user_account_fields' ); function audp_save_user_account_fields( $user_id ) {        /* Input Value: "0412 345 678"     * SHOULD Become: "0412345678"     */    $billing_phone = str_replace( array( ' ', '(', ')' ), '', $_POST['billing_phone'] );        if ( !empty( $_POST['billing_phone'] ) && preg_match( '/^04[0-9]{8}$/D', $billing_phone ) ) {                $billing_phone_query = get_users( array(             'meta_key' => 'billing_phone',            'meta_value' => $billing_phone,         ) );                foreach ( $billing_phone_query as $query ) {                        if ( $user_id == $query->ID ) {                                /* This value ($billing_phone) should be eg: "0412345678"                 * but remains "0412 345 678"                  */                update_user_meta( $user_id, 'billing_phone', $billing_phone );                        }                }        }}
查看完整描述

1 回答

?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊

更新

現(xiàn)在要過濾僅保留數(shù)字的電話號(hào)碼字符串,您可以更好地preg_replace()使用str_replace().

下面,帳單電話號(hào)碼在保存之前將被過濾:

  • WordPress 管理員用戶儀表板

  • 我的帳戶地址 編輯帳單地址

  • 下訂單后(保存數(shù)據(jù)之前)

代碼:

// In Wordpress user profile (increased hook priority)

add_action( 'personal_options_update', 'save_user_billing_phone_fields', 999999 );

add_action( 'edit_user_profile_update', 'save_user_billing_phone_fields', 999999 );

function save_user_billing_phone_fields( $user_id ) {

    $field_key = 'billing_phone';


    if( isset($_POST[$field_key]) && ! empty($_POST[$field_key]) ) {

        // Filtering billing phone (removing everything else than numbers)

        $billing_phone = preg_replace( '/[^0-9]/', '', sanitize_text_field($_POST[$field_key]) );


        // Update the billing phone

        update_user_meta( $user_id, 'billing_phone', $billing_phone );

    }

}


// On My account > edit addresses > Billing

add_action( 'woocommerce_process_myaccount_field_billing_phone', 'filter_my_account_billing_phone_fields' );

function filter_my_account_billing_phone_fields( $value ) {

    return preg_replace( '/[^0-9]/', '', $value );

}


// On order submission

add_action( 'woocommerce_checkout_posted_data', 'wc_checkout_posted_data_filter_callback' );

function  wc_checkout_posted_data_filter_callback( $data ) {

    $field_key = 'billing_phone';


    if( isset($data[$field_key]) ) {

        // Filtering billing phone (removing everything else than numbers)

        $data[$field_key] = preg_replace( '/[^0-9]/', '', $data[$field_key] );

    }

    return $data;

}

代碼位于活動(dòng)子主題(或活動(dòng)主題)的functions.php 文件中。經(jīng)過測(cè)試并有效。


查看完整回答
反對(duì) 回復(fù) 2023-07-21
  • 1 回答
  • 0 關(guān)注
  • 143 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)