Title: International Phone Number Format
Author: Mohamed Endisha
Published: <strong>2023 年 11 月 25 日</strong>
Last modified: 2023 年 11 月 25 日

---

搜尋外掛

![](https://ps.w.org/international-phone-number-format/assets/banner-772x250.png?
rev=3001614)

這個外掛**並未在最新的 3 個 WordPress 主要版本上進行測試**。開發者可能不再對這個
外掛進行維護或提供技術支援，並可能會與更新版本的 WordPress 產生使用上的相容性問題。

![](https://ps.w.org/international-phone-number-format/assets/icon-256x256.png?rev
=3001614)

# International Phone Number Format

 由 [Mohamed Endisha](https://profiles.wordpress.org/endisha/) 開發

[下載](https://downloads.wordpress.org/plugin/international-phone-number-format.1.0.0.zip)

 * [詳細資料](https://tw.wordpress.org/plugins/international-phone-number-format/#description)
 * [使用者評論](https://tw.wordpress.org/plugins/international-phone-number-format/#reviews)
 *  [安裝方式](https://tw.wordpress.org/plugins/international-phone-number-format/#installation)
 * [開發資訊](https://tw.wordpress.org/plugins/international-phone-number-format/#developers)

 [技術支援](https://wordpress.org/support/plugin/international-phone-number-format/)

## 外掛說明

The International Phone Number Format plugin empowers you to seamlessly format, 
validate, and store international phone numbers according to the E.164 standard.
This functionality spans across various sections of your WooCommerce-powered online
store and within WordPress itself. Notably, the plugin effectively formats and validates
phone numbers in key areas such as WooCommerce checkout fields, customer account
address fields, order details within the WordPress admin dashboard, and user edits
page in the WordPress admin dashboard.

The plugin dynamically adapts to the countries associated with billing and shipping
addresses in your WooCommerce settings and designates them as the countries for 
phone number formatting fields.

#### Features

 * Enables easy configuration of fields through the settings.
 * Automatically formats and validates phone numbers in WooCommerce checkout fields.
 * Automatic country detection based on the user’s IP address.
 * Supports automatic formatting and validation of phone numbers in customer account
   addresses.
 * Ensures automatic formatting and validation of phone numbers in Dashboard order
   details.
 * Provides automatic formatting and validation of phone numbers in Dashboard edit-
   user details.
 * Allows customization by setting specific fields as phone numbers for ease of 
   use.
 * Offers the flexibility to set additional custom Regex patterns for specific country
   codes using filters.
 * Stores phone numbers in the E.164 standard ([+][country code][phone number]) 
   for consistency and compatibility.

#### Requirements

 * WordPress 5.7 or newer.
 * WooCommerce 6.0 or newer.
 * PHP version 8.0 or newer.

### External Library Usage

This plugin utilizes the [intl-tel-input](https://github.com/jackocnr/intl-tel-input)
library to provide phone number formatting functionality.

### License

International Phone Number Format is licensed under the GNU General Public License
v2 or later.

## 螢幕擷圖

[⌊Plugin Settings⌉⌊Plugin Settings⌉[

Plugin Settings

[⌊Checkout Fields⌉⌊Checkout Fields⌉[

Checkout Fields

[⌊Address Fields⌉⌊Address Fields⌉[

Address Fields

[⌊Order Details⌉⌊Order Details⌉[

Order Details

## 安裝方式

#### Minimum Requirements

 * PHP 8.0 or greater is recommended
 * MySQL 5.6 or greater is recommended

#### Automatic installation

Automatic installation is the easiest option — WordPress will handles the file transfer,
and you won’t need to leave your web browser. To do an automatic install of International
Phone Number Format, log in to your WordPress dashboard, navigate to the Plugins
menu, and click “Add New.”

In the search field type “International Phone Number Format,” then click “Search
Plugins.” Once you’ve found us, you can view details about it such as the point 
release, rating, and description. Most importantly of course, you can install it
by! Click “Install Now,” and WordPress will take it from there.

#### Manual installation

Manual installation method requires downloading the International Phone Number Format
plugin and uploading it to your web server via your favorite FTP application. The
WordPress codex contains [instructions on how to do this here](https://wordpress.org/support/article/managing-plugins/#manual-plugin-installation).

## 常見問題集

### How do I enable and configure the international phone number format?

Once the plugin is activated, navigate to WooCommerce > Settings > International
Phone Number Format tab in your WordPress admin dashboard.

### Can I add new fields?

Yes, you can add new fields using the `intl_phone_number_format_fields` filter.

### How do I add a new custom WooCommerce checkout field?

Certainly. if you wish to add a Shipping Phone number field named `shipping_phone`
to the checkout fields, it will be automatically formatted using the international
phone number mask. If the field is set as required, it will undergo validation both
in the frontend and backend as a valid number. Here’s how you can achieve that:

    ```
    <?php
    add_filter('woocommerce_shipping_fields', function ($fields)
    {
        $fields['shipping_phone']['type'] = 'tel';
        $fields['shipping_phone']['label'] = __('Shipping Phone Number', 'woocommerce');
        $fields['shipping_phone']['class'] = array('form-row-wide');
        $fields['shipping_phone']['required'] = false;
        return $fields;
    }, 10, 1);
    ```

### How do I add a new custom field?

Of course, let’s say you want to add a “Mobile Phone Number” field to user account
details in WooCommerce:

Add the custom field using the `intl_phone_number_format_fields` filter. Here’s 
an example code snippet:

    ```
    <?php
    add_filter('intl_phone_number_format_fields', function ($fields){
        $fields[] = array(
            'id' => 'mobile',
            'enable' => true,
            'desc' => 'Mobile phone number on the profile page',
            'label' => 'Mobile',
            'frontend_validation' => true, // JS frontend validation
            'backend_validation' => true,  // Backend validation
            'countries' => 'all', // Countries related to fields (all, billing, or shipping)
            'type' => 'custom', // Field type (custom, billing, or shipping)
        );
        return $fields;
    });
    ```

Display the added field in the frontend “My Account” details section. Use the following
code:

    ```
    <?php
    add_action('woocommerce_edit_account_form_start', function () {
        $user = wp_get_current_user();
        ?>
            <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
                <label for="mobile"><?php _e('Mobile phone', 'woocommerce'); ?>
                    <span class="required">*</span></label>
                <input type="text" class="woocommerce-Input woocommerce-Input--phone input-text" name="mobile" id="mobile" value="<?php echo esc_attr($user->mobile); ?>" />
            </p>
        <?php
    });
    ```

Implement validation for the field. Use the woocommerce_save_account_details_errors
action hook along with the intl validation service:

    ```
    <?php
    add_action('woocommerce_save_account_details_errors', function (WP_Error $errors) {
        $fields = (new IPNFP_Fields_Service)->get_fields();
        $validated = (new IPNFP_Validate_Service)->validate($fields);
        if (!empty($validated)) {
            foreach ($validated as $field => $error) {
                $errors->add($field, $error);
            }
        }
    }, 10, 1);
    ```

Save the user’s input for the field:

    ```
    <?php
    add_action('woocommerce_save_account_details', function (int $user_id) {
        if (isset($_POST['mobile'])) {
            $mobile = sanitize_text_field($_POST['mobile']);
            update_user_meta($user_id, 'mobile', $mobile);
        }
    }, 10, 1);
    ```

In case the JS file is not included, you can include it using:

    ```
    <?php
    add_filter('intl_phone_number_format_validate_enqueue_js', function ($valid){
        if (is_account_page()) {
            $valid = true;
        }
        return $valid;
    });
    ```

By following these steps, the field should be included and formatted with the International
Phone Number Format mask.

### How to Add Additional Validation for Specific Country Numbers

You have the flexibility to add custom validation using regular expressions for 
specific countries. If you need to enforce a specific format for phone numbers in
a particular country, you can achieve this by the intl_phone_number_format_custom_country_prefixes_validation
filter.

Here’s an example scenario: Let’s assume you want to apply custom validation rules
for Libyan phone numbers, allowing only a specific format. In this case, you can
utilize the following filter to associate the country calling code with the regular
expression pattern:

    ```
    <?php
    // The structure should follow this format:
    // 'calling code' => 'regular expression pattern'
    add_filter('intl_phone_number_format_custom_country_prefixes_validation', function ($patterns) {
        $patterns['+218'] = '^\+2189[1-6][0-9]{7}$';
        return $patterns;
    });
    ```

### How do I modify fields?

The plugin provides several filters that allow you to modify the fields, including
built-in fields such as `billing_phone`, `_billing_phone`, `shipping_phone`, and`
_shipping_phone`, as well as custom fields that you added using `intl_phone_number_format_fields`
filter.

This filter allows you to enable or disable the field.

    ```
    intl_phone_number_format_modify_{FIELD}_enable
    ```

This filter allows you to enable or disable JS frontend validation for the specified
field.

    ```
    intl_phone_number_format_modify_{FIELD}_frontend_validation
    ```

This filter to enable or disable backend validation for the specified field.

    ```
    intl_phone_number_format_modify_{FIELD}_backend_validation
    ```

This filter to adjust the countries that will be shown as calling codes for the 
field. Accepted values are: `all`, `billing`, and `shipping`.

    ```
    intl_phone_number_format_modify_{FIELD}_countries
    ```

This filter to define the field type. For example, if the field is related to billing,
set it as `billing`. If it’s for shipping, set it as `shipping`. Otherwise, set 
it as `custom`.

    ```
    intl_phone_number_format_modify_{FIELD}_type
    ```

## 使用者評論

![](https://secure.gravatar.com/avatar/aee55e753a544d38b8a0ecaeeb91c7ff4a6d42e77fdb5dd2d5ff5bec9df3fbb4?
s=60&d=retro&r=g)

### 󠀁[Best one yet, now lets make it perfect!](https://wordpress.org/support/topic/best-one-yet-now-lets-make-it-perfect/)󠁿

 [linuxas99](https://profiles.wordpress.org/linuxas99/) 2024 年 7 月 30 日 2 則留言

I've tested at least six plugins that use the jackocnr library, and none of them
are perfect yet. However, I just came across your version and can confidently say
it's the best one so far! The code is clean, and the backend settings GUI is very
impressive, especially considering it's only an initial release (v1.0.0). Mohamed,
I sent you a direct message and would really like to chat with you for a bit. I'm
willing to suggest some improvements and even donate if you're open to implementing
them. I believe we can make this plugin a "must-have" for all international WooCommerce
stores, as there isn't a good option on the market yet. Your plugin has tremendous
potential. Thanks!

![](https://secure.gravatar.com/avatar/135015d6311e2f0eca9d227855a18266812e55801a03dfab753dfc75077a08ff?
s=60&d=retro&r=g)

### 󠀁[Works as expected! Thank you](https://wordpress.org/support/topic/works-as-expected-thank-you-6/)󠁿

 [Minhaz Irphan Mohamed](https://profiles.wordpress.org/minhazmohamed/) 2024 年 
3 月 20 日

Love this plugin, works as expected. Highly recommended!

 [ 閱讀全部 2 則使用者評論 ](https://wordpress.org/support/plugin/international-phone-number-format/reviews/)

## 參與者及開發者

以下人員參與了開源軟體〈International Phone Number Format〉的開發相關工作。

參與者

 *   [ Mohamed Endisha ](https://profiles.wordpress.org/endisha/)

[將〈International Phone Number Format〉外掛本地化為台灣繁體中文版](https://translate.wordpress.org/projects/wp-plugins/international-phone-number-format)

### 對開發相關資訊感興趣？

任何人均可[瀏覽程式碼](https://plugins.trac.wordpress.org/browser/international-phone-number-format/)、
查看 [SVN 存放庫](https://plugins.svn.wordpress.org/international-phone-number-format/)，
或透過 [RSS](https://plugins.trac.wordpress.org/log/international-phone-number-format/?limit=100&mode=stop_on_copy&format=rss)
訂閱[開發記錄](https://plugins.trac.wordpress.org/log/international-phone-number-format/)。

## 變更記錄

#### 1.0.0 2023-11-25

 * Initial release.

## 中繼資料

 *  版本 **1.0.0**
 *  最後更新 **3 年前**
 *  啟用安裝數 **300+**
 *  WordPress 版本需求 ** 6.0 或更新版本 **
 *  已測試相容的 WordPress 版本 **6.3.14**
 *  PHP 版本需求 ** 8.0 或更新版本 **
 *  語言
 * [English (US)](https://wordpress.org/plugins/international-phone-number-format/)
 * 標籤:
 * [format](https://tw.wordpress.org/plugins/tags/format/)[international](https://tw.wordpress.org/plugins/tags/international/)
   [mask](https://tw.wordpress.org/plugins/tags/mask/)[number](https://tw.wordpress.org/plugins/tags/number/)
   [phone](https://tw.wordpress.org/plugins/tags/phone/)
 *  [進階檢視](https://tw.wordpress.org/plugins/international-phone-number-format/advanced/)

## 評分

 5 星，滿分為 5 星

 *  [  2 個 5 星使用者評論     ](https://wordpress.org/support/plugin/international-phone-number-format/reviews/?filter=5)
 *  [  0 個 4 星使用者評論     ](https://wordpress.org/support/plugin/international-phone-number-format/reviews/?filter=4)
 *  [  0 個 3 星使用者評論     ](https://wordpress.org/support/plugin/international-phone-number-format/reviews/?filter=3)
 *  [  0 個 2 星使用者評論     ](https://wordpress.org/support/plugin/international-phone-number-format/reviews/?filter=2)
 *  [  0 個 1 星使用者評論     ](https://wordpress.org/support/plugin/international-phone-number-format/reviews/?filter=1)

[撰寫評分](https://wordpress.org/support/plugin/international-phone-number-format/reviews/#new-post)

[查看全部使用者評論](https://wordpress.org/support/plugin/international-phone-number-format/reviews/)

## 參與者

 *   [ Mohamed Endisha ](https://profiles.wordpress.org/endisha/)

## 技術支援

使用者可在技術支援論壇提出意見反應或使用問題。

 [檢視技術支援論壇](https://wordpress.org/support/plugin/international-phone-number-format/)