<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WordPress開發教學 &#8211; OctopusWP | WordPress客製開發專家</title>
	<atom:link href="https://www.octopuswp.com/category/wordpress%E9%96%8B%E7%99%BC%E6%95%99%E5%AD%B8/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.octopuswp.com</link>
	<description>外掛，佈景主題客製化開發服務，形象、電子商務網站建置，並提供最優質的外掛軟體</description>
	<lastBuildDate>Thu, 16 Dec 2021 13:43:47 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.6.14</generator>

<image>
	<url>https://www.octopuswp.com/wp-content/uploads/2020/02/cropped-icon-32x32.png</url>
	<title>WordPress開發教學 &#8211; OctopusWP | WordPress客製開發專家</title>
	<link>https://www.octopuswp.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>初始化一個WordPress外掛</title>
		<link>https://www.octopuswp.com/2020/02/25/%e5%88%9d%e5%a7%8b%e5%8c%96%e4%b8%80%e5%80%8bwordpress%e5%a4%96%e6%8e%9b/</link>
					<comments>https://www.octopuswp.com/2020/02/25/%e5%88%9d%e5%a7%8b%e5%8c%96%e4%b8%80%e5%80%8bwordpress%e5%a4%96%e6%8e%9b/#respond</comments>
		
		<dc:creator><![CDATA[OctopusWP]]></dc:creator>
		<pubDate>Tue, 25 Feb 2020 09:59:42 +0000</pubDate>
				<category><![CDATA[WordPress開發教學]]></category>
		<category><![CDATA[初級]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[技術]]></category>
		<guid isPermaLink="false">https://www.octopuswp.com/?p=1634</guid>

					<description><![CDATA[<p>呈上篇 &#8220;&#8221; 當外掛檔案被啟用並載入後，就可以在主檔案內開始撰寫PHP程式碼 Word [&#8230;]</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/25/%e5%88%9d%e5%a7%8b%e5%8c%96%e4%b8%80%e5%80%8bwordpress%e5%a4%96%e6%8e%9b/">初始化一個WordPress外掛</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>呈上篇 &#8220;<a href="https://www.octopuswp.com/2020/02/25/how-to-create-a-wordpress-plugin/">在WordPress中建立外掛的方法</a>&#8221;</p>
<p>當外掛檔案被啟用並載入後，就可以在主檔案內開始撰寫PHP程式碼</p>
<p>WordPress並沒有規定要如何架構外掛程式碼(檔案架構、模組化等等)，但有給出一個Code Style的規範，可參考 <a href="https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/" target="_blank" rel="noopener">PHP Coding Standards, wordpress.org</a></p>
<p>可以直接就執行一些動作</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="droide">&lt;?php
// register hook
add_action('wp_head', 'octopuswp_foo_bar');
function octopuswp_foo_bar()
{
    // do something in hooked function
}

// do something
$order = wc_get_order(123);
$order-&gt;update_meta_data('_foo', 'bar');
$order-&gt;save();
</pre>
<p>但，這樣的作法可能會有問題，以上列程式碼為例</p>
<ol>
<li>octopuswp_foo_bar 定義在全域下，可能會與其他第三方外掛的名稱衝突</li>
<li>用到了WooCommerce的wc_get_order，如果你的外掛在WooCommerce之前被載入，那這行就會出錯，因為找不到<code class="EnlighterJSRAW" data-enlighter-language="null">wc_get_order</code>這個function，WordPress會依照資料夾名稱順序去載入外掛</li>
</ol>
<p>考慮到WordPress的生態系，會用其他第三方外掛，都命名在全域下，會有名稱衝突的風險，因此最好的做法是OOP，將外掛包成一個類別</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="droide">&lt;?php
// 判斷類別是否已經存在這個Class
if(!class_exists('OctopusWP_Plugin')) {
    class OctopusWP_Plugin
    {
        public function __construct()
        {
            $this-&gt;register_hooks();
            $order = wc_get_order(123);
        }

        /**
         * 註冊所有hooks
         */
        private function register_hooks()
        {
            // hook後面給的都是一個"可被呼叫的"的函式名稱，因為foo_bar是這個類別的方法，所以必須要傳入這個類別的實體(instance)，才可在外部被呼叫，且foo_bar必須為public
            add_action('wp_head', [$this, 'foo_bar']);
        }

        public function foo_bar()
        {
            // do something
        }
    }
    // 初始化
    new OctopusWP_Plugin();
}</pre>
<p>透過類別，來限制外掛的命名域，類別內方法重複並沒有關係，只要類別名稱不重複就好</p>
<p>呈上述問題2，這樣直接new出來的方式，例如上面程式碼的建構式內還是有用到了<code class="EnlighterJSRAW" data-enlighter-language="null">wc_get_order</code>，如果外掛在WooCommerce前被載入的話，還是會出錯，假如你的外掛是依賴在某個外掛下面，例如是一個WooCommerce用的金流外掛或是物流外掛，那最好是確保你的外掛載入在WooCommerce後，可以使用</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="droide">&lt;?php
// 判斷類別是否已經存在這個Class
if(!class_exists('OctopusWP_Plugin')) {
    class OctopusWP_Plugin
    {
        // 存放外掛類別的instance
        public static $instance;

        // 取得此類別的instance
        public static function get_instance()
        {
            // 判斷是否有初始化過
            if(is_null(self::$instance)) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        public function __construct()
        {
            $this-&gt;register_hooks();
            $order = wc_get_order(123);
        }

        /**
         * 註冊所有hooks
         */
        private function register_hooks()
        {
            // hook後面給的都是一個"可被呼叫的"的函式名稱，因為foo_bar是這個類別的方法，所以必須要傳入這個類別的實體(instance)，才可在外部被呼叫，且foo_bar必須為public
            add_action('wp_head', [$this, 'foo_bar']);
        }

        public function foo_bar()
        {
            // do something
        }
    }
    // 在特定的hook時間點再初始化外掛
    // WooCommerce載入完之後會呼叫的action hook
    add_action('woocommerce_init', ['OctopusWP_Plugin', 'get_instance']); // 因為get_instance是static函式，所以只要給類別的名稱就行
    // 所有外掛都被載入完之後會呼叫的action hook
    add_action('plugins_loaded', ['OctopusWP_Plugin', 'get_instance']);
}</pre>
<p>初始化的方法改成<code class="EnlighterJSRAW" data-enlighter-language="null">get_instance</code>的方式，可以直接定義在類別內並傳入action hook</p>
<p>初始化的時機點也不是在外掛檔案被載入時馬上初始化，而是再hook到某一個action，在action被呼叫時再初始化</p>
<p>當然，如果你的外掛沒有任何依賴任何其他外掛，或者下列情形，就是直接初始化就好</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="droide">// 判斷類別是否已經存在這個Class
if(!class_exists('OctopusWP_Plugin')) {
    class OctopusWP_Plugin
    {
        // 存放外掛類別的instance
        public static $instance;

        // 取得此類別的instance
        public static function get_instance()
        {
            // 判斷是否有初始化過
            if(is_null(self::$instance)) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        public function __construct()
        {
            $this-&gt;register_hooks();
        }

        /**
         * 註冊所有hooks
         */
        private function register_hooks()
        {
            // hook後面給的都是一個"可被呼叫的"的函式名稱，因為foo_bar是這個類別的方法，所以必須要傳入這個類別的實體(instance)，才可在外部被呼叫，且foo_bar必須為public
            add_action('wp_head', [$this, 'foo_bar']);
        }

        public function foo_bar()
        {
            // do something

            // 雖然這裡有用到wc_get_order，但是foo_bar方法是在wp_head被呼叫時才會呼叫的，而這時WooCommerce已經被載入完畢了，所以不會出錯
            $order = wc_get_order(123);
        }
    }
    OctopusWP_Plugin::get_instance();
}</pre>
<p>總之，這裡只要注意一下程式有載入順序，在外掛初始化時不能呼叫還沒被定義的方法或類別</p>
<p>但如果是在被hook的方法內，因在註冊hook時只會紀錄hook上去的方法資訊，在實際hook被呼叫時才會執行該方法</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/25/%e5%88%9d%e5%a7%8b%e5%8c%96%e4%b8%80%e5%80%8bwordpress%e5%a4%96%e6%8e%9b/">初始化一個WordPress外掛</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.octopuswp.com/2020/02/25/%e5%88%9d%e5%a7%8b%e5%8c%96%e4%b8%80%e5%80%8bwordpress%e5%a4%96%e6%8e%9b/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>在WordPress中建立外掛的方法</title>
		<link>https://www.octopuswp.com/2020/02/25/how-to-create-a-wordpress-plugin/</link>
					<comments>https://www.octopuswp.com/2020/02/25/how-to-create-a-wordpress-plugin/#respond</comments>
		
		<dc:creator><![CDATA[OctopusWP]]></dc:creator>
		<pubDate>Tue, 25 Feb 2020 07:14:21 +0000</pubDate>
				<category><![CDATA[WordPress開發教學]]></category>
		<category><![CDATA[初級]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[技術]]></category>
		<guid isPermaLink="false">https://www.octopuswp.com/?p=1623</guid>

					<description><![CDATA[<p>在WordPress中，外掛，是將一項功能的程式碼，將他集中起來放在同一個資料夾下，可以隨時啟用或是停用 1. [&#8230;]</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/25/how-to-create-a-wordpress-plugin/">在WordPress中建立外掛的方法</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>在WordPress中，外掛，是將一項功能的程式碼，將他集中起來放在同一個資料夾下，可以隨時啟用或是停用</p>
<h2>1. 建立外掛資料夾</h2>
<p>首先，在WordPress中，外掛的資料夾會放在<code class="EnlighterJSRAW" data-enlighter-language="null">/wp-content/plugins</code>下，WordPress會將每一個資料夾視為一個外掛(而非外掛名稱)</p>
<p>所以第一步就是在/wp-content/plugins下建立一個資料夾</p>
<h2>2. 建立外掛主檔案</h2>
<p>WordPress會到外掛資料夾下找尋符合以下條件的PHP檔案</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="droide">&lt;?php
/**
 * Plugin Name: OctopusWP Demo Plugin
 */</pre>
<p>當檔案內有PHP註解並包含 <code class="EnlighterJSRAW" data-enlighter-language="null">Plugin Name:</code>的，就會是一個有效的外掛主檔案(檔案名稱隨意)；後面加上外掛名稱後，就會出現在外掛列表上</p>
<p><img loading="lazy" class="alignnone size-full wp-image-1629" src="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1623.png" alt="" width="864" height="143" srcset="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1623.png 864w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1623-300x50.png 300w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1623-768x127.png 768w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1623-265x44.png 265w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1623-504x83.png 504w" sizes="(max-width: 864px) 100vw, 864px" /></p>
<p>當按下啟用後，WordPress就會將這個檔案載入，</p>
<p>經測試，一個外掛下可放好幾個外掛主檔案，但實務上，還是一個資料夾一個外掛主檔案為原則</p>
<p>其他資訊如描述、作者、作者網址、外掛網址、版本：</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="droide">&lt;?php
/**
 * Plugin Name: OctopusWP Demo Plugin
 * Description: 外掛描述
 * Author: OctopusWP
 * Author URI: https://www.octopuswp.com
 * Plugin URI: https://www.octopuwsp.com
 * Version: 1.0.0
 */</pre>
<p>在外掛列表呈現的結果：</p>
<p><img loading="lazy" class="alignnone size-full wp-image-1632" src="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1623-1.png" alt="" width="957" height="144" srcset="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1623-1.png 957w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1623-1-300x45.png 300w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1623-1-768x116.png 768w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1623-1-265x40.png 265w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1623-1-504x76.png 504w" sizes="(max-width: 957px) 100vw, 957px" /></p>
<p>&nbsp;</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/25/how-to-create-a-wordpress-plugin/">在WordPress中建立外掛的方法</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.octopuswp.com/2020/02/25/how-to-create-a-wordpress-plugin/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>加入自訂欄位至WordPress後台列表頁</title>
		<link>https://www.octopuswp.com/2020/02/21/add-custom-column-to-admin-list-table/</link>
					<comments>https://www.octopuswp.com/2020/02/21/add-custom-column-to-admin-list-table/#respond</comments>
		
		<dc:creator><![CDATA[OctopusWP]]></dc:creator>
		<pubDate>Fri, 21 Feb 2020 00:00:02 +0000</pubDate>
				<category><![CDATA[WordPress開發教學]]></category>
		<category><![CDATA[初級]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[技術]]></category>
		<guid isPermaLink="false">https://www.octopuswp.com/?p=1604</guid>

					<description><![CDATA[<p>WordPress作為內容管理系統(CMS)在前台依靠佈景主題產生各種內容頁面的HTML結構，在後台頁面則有自 [&#8230;]</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/21/add-custom-column-to-admin-list-table/">加入自訂欄位至WordPress後台列表頁</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>WordPress作為內容管理系統(CMS)在前台依靠佈景主題產生各種內容頁面的HTML結構，在後台頁面則有自己管理的HTML結構</p>
<p>各種內容的列表頁的表格即是，無論是商品、文章、頁面、作品等等，只要向WordPress註冊了一個文章類型(Post type)，WordPress就會自動幫你產生一個相關的列表管理頁，讓你可管理自訂文章類型的內容，而不用再自己寫管理頁面。</p>
<p>例如WooCommerce提供的shop_order(訂單)這個文章類型的列表頁</p>
<p><img loading="lazy" class="alignnone size-full wp-image-1608" src="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604.png" alt="" width="2340" height="525" srcset="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604.png 2340w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-300x67.png 300w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-1024x230.png 1024w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-768x172.png 768w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-1536x345.png 1536w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-2048x459.png 2048w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-265x59.png 265w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-504x113.png 504w" sizes="(max-width: 2340px) 100vw, 2340px" /></p>
<p>之後再來細談列表頁的產生機制，和如何產生自訂內容的列表頁(使用<code class="EnlighterJSRAW" data-enlighter-language="null">WP_List_Table</code>)</p>
<p>有時候要在列表頁進行一些快速操作或是顯示一些資訊，而不用再點進去編輯頁面看，就會需要在列表頁加上自訂欄位</p>
<p>如本站開發的&#8221;<a href="https://www.octopuswp.com/product/octopuswp-wc-ecpay-e-invoice-pro/">WooCommerce 綠界科技B2C電子發票模組專業版</a>&#8220;這個外掛，需要在訂單列表內加入一個電子發票欄位，以便進行快速操作</p>
<p>以下示範如何在WooCommerce的商品列表內加入自訂欄位，並顯示商品的重量資訊</p>
<h2>1. 加入自訂欄位</h2>
<p>使用的filter hook為<code class="EnlighterJSRAW" data-enlighter-language="null">manage_{$post_type}_posts_columns</code>，$post_type為文章類型，可在某個特定的文章類型列表中加入一個自訂欄位</p>
<p>如果要在所有文章類型的列表中加入，使用<code class="EnlighterJSRAW" data-enlighter-language="null">manage_posts_columns</code></p>
<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="droide">&lt;?php
// 商品的post type為product
add_filter('manage_product_posts_columns', 'octopuswp_add_product_weight_column');

/**
 * 加入重量欄位
 * @param array $columns 欄位資訊
 * @return array
 */
function octopuswp_add_product_weight_column($columns)
{
    // $columns會傳入所有欄位的資訊，只需要新增進去即可
    // product_weight為自訂的key
    $columns['product_weight'] = '重量';
    return $columns;
}
?&gt;</pre>
<p><img loading="lazy" class="size-full wp-image-1610 alignnone" src="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-1.png" alt="" width="2104" height="393" srcset="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-1.png 2104w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-1-300x56.png 300w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-1-1024x191.png 1024w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-1-768x143.png 768w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-1-1536x287.png 1536w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-1-2048x383.png 2048w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-1-265x49.png 265w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-1-504x94.png 504w" sizes="(max-width: 2104px) 100vw, 2104px" /></p>
<h2>2. 加入自訂欄位的資料</h2>
<p>加入欄位之後，再來就是要把欄位資料放上去，使用<code class="EnlighterJSRAW" data-enlighter-language="null">manage_{$post_type}_posts_custom_column</code>這個action hook</p>
<p>如果不指定文章類型，則使用<code class="EnlighterJSRAW" data-enlighter-language="null">manage_posts_custom_column</code></p>
<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="droide">&lt;?php
// 商品的post type為product
add_action('manage_product_posts_custom_column', 'octopuswp_print_product_weight_to_column', 10, 2);

/**
 * 加入重量欄位的資料
 * @param string $column_name 欄位名稱
 * @param int $post_id 文章ID
 */
function octopuswp_print_product_weight_to_column($column_name, $post_id)
{
    // WordPress在對於表格的每一行與每個欄位都會呼叫一次這個action hook，並將當下的欄位名稱及文章ID傳入
    // 所以要判斷當下的欄位是不是剛剛新增的，如果是的話，再顯示資料
    // product_weight為剛剛新增的欄位key
    if('product_weight' == $column_name) {
        $product = wc_get_product($post_id);
        echo $product-&gt;get_weight();
    }
}
?&gt;</pre>
<p><img loading="lazy" class="alignnone size-full wp-image-1614" src="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-2.png" alt="" width="689" height="293" srcset="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-2.png 689w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-2-300x128.png 300w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-2-265x113.png 265w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1604-2-504x214.png 504w" sizes="(max-width: 689px) 100vw, 689px" /></p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/21/add-custom-column-to-admin-list-table/">加入自訂欄位至WordPress後台列表頁</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.octopuswp.com/2020/02/21/add-custom-column-to-admin-list-table/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress如何將後端資料傳給自訂的Javascript檔案內使用</title>
		<link>https://www.octopuswp.com/2020/02/20/%e5%a6%82%e4%bd%95%e5%b0%87%e5%be%8c%e7%ab%af%e8%b3%87%e6%96%99%e5%82%b3%e7%b5%a6%e8%87%aa%e8%a8%82%e7%9a%84javascript%e6%aa%94%e6%a1%88%e5%85%a7%e4%bd%bf%e7%94%a8/</link>
					<comments>https://www.octopuswp.com/2020/02/20/%e5%a6%82%e4%bd%95%e5%b0%87%e5%be%8c%e7%ab%af%e8%b3%87%e6%96%99%e5%82%b3%e7%b5%a6%e8%87%aa%e8%a8%82%e7%9a%84javascript%e6%aa%94%e6%a1%88%e5%85%a7%e4%bd%bf%e7%94%a8/#respond</comments>
		
		<dc:creator><![CDATA[OctopusWP]]></dc:creator>
		<pubDate>Thu, 20 Feb 2020 07:24:19 +0000</pubDate>
				<category><![CDATA[WordPress開發教學]]></category>
		<category><![CDATA[初級]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[技術]]></category>
		<guid isPermaLink="false">https://www.octopuswp.com/?p=1589</guid>

					<description><![CDATA[<p>呈上篇 &#8220;&#8221; 在寫Javascript的過程中，有時會遇到需要將後端的資料傳到Java [&#8230;]</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/20/%e5%a6%82%e4%bd%95%e5%b0%87%e5%be%8c%e7%ab%af%e8%b3%87%e6%96%99%e5%82%b3%e7%b5%a6%e8%87%aa%e8%a8%82%e7%9a%84javascript%e6%aa%94%e6%a1%88%e5%85%a7%e4%bd%bf%e7%94%a8/">WordPress如何將後端資料傳給自訂的Javascript檔案內使用</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>呈上篇 &#8220;<a href="https://www.octopuswp.com/2020/02/20/how-to-enqueue-css-js-file-in-wordpress/">如何在WordPress中正確引入外部的CSS或Javascript檔案</a>&#8221;</p>
<p>在寫Javascript的過程中，有時會遇到需要將後端的資料傳到Javascript內的問題，如多國語系字串、設定檔等，讓Javascript可以使用這些資料</p>
<p>多國語系字串是最常見的情境，因為WordPress的翻譯機制依賴後端處理mo檔案來產生本地化字串，如果你的Javascript內需要控制顯示某些訊息，但這些訊息又要跟著翻譯檔案來翻譯成本地化訊息的話，就會需要特別處理過這個訊息的取得方式</p>
<h2>1. 在Javascript內使用翻譯過後的字串</h2>
<p>使用<code class="EnlighterJSRAW" data-enlighter-language="null">wp_localize_script</code>這個function，在註冊過script之後定義需要傳入的資料以及資料的物件名稱</p>
<p><code class="EnlighterJSRAW" data-enlighter-language="null">wp_localize_script</code>有3個參數：</p>
<ul>
<li>handle，需要套用的Javascript檔案handle名稱</li>
<li>object_name，Javascript全域物件名稱，WordPress會將這些資訊包在這個物件內</li>
<li>data，需要傳入的資料</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="droide">&lt;?php
add_action('wp_enqueue_scripts', 'octopuswp_enqueue_scripts');
/**
 * 傳送後端資料到Javascript
 */
function octopuswp_enqueue_scripts()
{
    // 取得子主題的目錄URL

    $stylesheet_dir_uri = get_stylesheet_directory_uri();

    // 註冊Javascript

    wp_register_script('octopuswp-single-post', $stylesheet_dir_uri . '/assets/js/single-post.js', ['jquery']);

    // 註冊此Javascript的需要用到的資料
    
    wp_localize_script('octopuswp-single-post', 'OctopusWP_Single_Post', [
        // 使用翻譯函式來翻譯字串Hello World
        'helloWorld' =&gt; __('Hello World', 'octopuswp')
    ]);

    // 引入(enqueue)
    
    if(is_single() &amp;&amp; 'post' == get_post_type()) {
        wp_enqueue_script('octopuswp-single-post');
    }
}
?&gt;</pre>
<p>WordPress會在這個Javascript被引入(enqueue)時，在html下產生一個全域的Javascript物件，再將資訊放在裡面</p>
<p><img loading="lazy" class="alignnone size-full wp-image-1600" src="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1589.png" alt="" width="1260" height="118" srcset="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1589.png 1260w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1589-300x28.png 300w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1589-1024x96.png 1024w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1589-768x72.png 768w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1589-265x25.png 265w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1589-504x47.png 504w" sizes="(max-width: 1260px) 100vw, 1260px" /></p>
<p>因此在Javascript內就可以使用OctopusWP_Single_Post物件去存取裡面的資訊</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">alert(OctopusWP_Single_Post.helloWorld)</pre>
<p>當然<code class="EnlighterJSRAW" data-enlighter-language="null">wp_localize_script</code>內可傳入的不只是本地化字串，任何資訊都行，端看需求而定</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/20/%e5%a6%82%e4%bd%95%e5%b0%87%e5%be%8c%e7%ab%af%e8%b3%87%e6%96%99%e5%82%b3%e7%b5%a6%e8%87%aa%e8%a8%82%e7%9a%84javascript%e6%aa%94%e6%a1%88%e5%85%a7%e4%bd%bf%e7%94%a8/">WordPress如何將後端資料傳給自訂的Javascript檔案內使用</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.octopuswp.com/2020/02/20/%e5%a6%82%e4%bd%95%e5%b0%87%e5%be%8c%e7%ab%af%e8%b3%87%e6%96%99%e5%82%b3%e7%b5%a6%e8%87%aa%e8%a8%82%e7%9a%84javascript%e6%aa%94%e6%a1%88%e5%85%a7%e4%bd%bf%e7%94%a8/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>如何在WordPress中正確引入外部的CSS或Javascript檔案</title>
		<link>https://www.octopuswp.com/2020/02/20/how-to-enqueue-css-js-file-in-wordpress/</link>
					<comments>https://www.octopuswp.com/2020/02/20/how-to-enqueue-css-js-file-in-wordpress/#comments</comments>
		
		<dc:creator><![CDATA[OctopusWP]]></dc:creator>
		<pubDate>Thu, 20 Feb 2020 06:44:05 +0000</pubDate>
				<category><![CDATA[WordPress開發教學]]></category>
		<category><![CDATA[初級]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[技術]]></category>
		<guid isPermaLink="false">https://www.octopuswp.com/?p=1575</guid>

					<description><![CDATA[<p>在WordPress客製化開發過程中，也免不了接觸到前端的開發需求，這時就會有需要引入外部CSS檔案或是Jav [&#8230;]</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/20/how-to-enqueue-css-js-file-in-wordpress/">如何在WordPress中正確引入外部的CSS或Javascript檔案</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>在WordPress客製化開發過程中，也免不了接觸到前端的開發需求，這時就會有需要引入外部CSS檔案或是Javascript檔案的時候</p>
<p>如果是自己寫的程式碼，有的人會選擇將程式碼寫在後台主題或外掛提供的自訂CSS或Javascript設定內，但對於有版本控制需求的人，還是必須寫在檔案內才能控制</p>
<h2>1. 引入外部CSS檔案或JS檔案</h2>
<p>可以使用兩個hook：</p>
<p><code class="EnlighterJSRAW" data-enlighter-language="null">wp_enqueue_scripts</code>：前台使用</p>
<p><code class="EnlighterJSRAW" data-enlighter-language="null">admin_enqueue_scripts</code>：後台使用</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="droide">&lt;?php
add_action('wp_enqueue_scripts', 'octopuswp_enqueue_scripts');
/**
 * 引入外部CSS或JS檔案
 * 以下示範在單一文章頁面引入自訂的Javascript與CSS檔案
 * 已經在子主題assets/js放入了single-post.js檔案，在assets/css放入了single-post.css檔案
 */
function octopuswp_enqueue_scripts()
{
    // 取得子主題的目錄URL
    
    $stylesheet_dir_uri = get_stylesheet_directory_uri();
    
    // 通常分成兩個步驟
    // 1. 先向WordPress註冊你的檔案名稱，檔案來源

    // 註冊Javascript檔案，使用wp_register_script這個function
    // wp_register_script有五個參數：
    // handle，自訂的唯一名稱
    // src，檔案的來源url，可以是本站URL，可以是外部CDN URL
    //   如果是放在子主題，可以用 get_stylesheet_directory_uri()來取得子主題資料夾的url
    //   如果是放在外掛，用plugin_dir_url()，來取得外掛目錄的url
    // dependencies，這個檔案跟依賴哪些檔案，WordPress會幫你依序引入，填入的是其他已註冊好的檔案的handle，WordPress已經有內建一些可以用，如jquery
    //   例如，你的Javascript檔案裡面有用到jquery，那就必須將他引入在jquery後面，否則會發生找不到jquery的錯誤
    // version，這個檔案的版本，WordPress在引入時會自動加上版本的參數，如果有自訂檔案版本，可填，不填的話會自動帶入WordPress版本
    // in_footer，是否是在footer(&lt;/body&gt;前)引入，否則的話會引入在head，預設是false

    wp_register_script('octopuswp-single-post', $stylesheet_dir_uri . '/assets/js/single-post.js', ['jquery']);

    // 註冊CSS檔案，使用wp_register_style這個function，參數跟wp_register_script差不多唯獨第五個參數不同
    // media，用在html &lt;link&gt;標籤的media屬性，可定義不同的media typ，預設是all

    wp_register_style('octopuswp-single-post', $stylesheet_dir_uri . '/assets/css/single-post.css');

    // 2. 正式引入
    // 使用wp_enqueue_script與wp_enqueue_style直接傳入handle名稱即可，如此就會在所有頁面中引入

    wp_enqueue_script('octopuswp-single-post');
    wp_enqueue_style('octopuswp-single-post');

    // 如果是要在特定頁面中引入，可以加入判斷條件
    // 當頁面為單一內容頁及文章類型為post的時候引入
    
    if(is_single() &amp;&amp; 'post' == get_post_type()) {
        wp_enqueue_script('octopuswp-single-post');
        wp_enqueue_style('octopuswp-single-post');
    }
}</pre>
<p>無論是前台的<code class="EnlighterJSRAW" data-enlighter-language="null">wp_enqueue_scripts</code>，或是後台的<code class="EnlighterJSRAW" data-enlighter-language="null">admin_enqueue_scripts</code>用法都一樣</p>
<p>唯獨要注意的是，這兩個是分開的，在前台hook註冊的只能在前台引入，在後台hook註冊的只能在後台引入</p>
<p>&nbsp;</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/20/how-to-enqueue-css-js-file-in-wordpress/">如何在WordPress中正確引入外部的CSS或Javascript檔案</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.octopuswp.com/2020/02/20/how-to-enqueue-css-js-file-in-wordpress/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>WooCommerce驗證結帳欄位的方法</title>
		<link>https://www.octopuswp.com/2020/02/17/woocommerce%e9%a9%97%e8%ad%89%e7%b5%90%e5%b8%b3%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/</link>
					<comments>https://www.octopuswp.com/2020/02/17/woocommerce%e9%a9%97%e8%ad%89%e7%b5%90%e5%b8%b3%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/#respond</comments>
		
		<dc:creator><![CDATA[OctopusWP]]></dc:creator>
		<pubDate>Mon, 17 Feb 2020 03:24:15 +0000</pubDate>
				<category><![CDATA[WordPress開發教學]]></category>
		<category><![CDATA[初級]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[技術]]></category>
		<guid isPermaLink="false">https://www.octopuswp.com/?p=1516</guid>

					<description><![CDATA[<p>在WordPress中想要用程式的方法來驗證WooCommerce的結帳欄位，這裡指的驗證方法是後端(PHP) [&#8230;]</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/17/woocommerce%e9%a9%97%e8%ad%89%e7%b5%90%e5%b8%b3%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/">WooCommerce驗證結帳欄位的方法</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>在WordPress中想要用程式的方法來驗證WooCommerce的結帳欄位，這裡指的驗證方法是後端(PHP)驗證</p>
<h2>1. 預設驗證規則</h2>
<p>WooCommerce會先取得結帳欄位的參數(可透過<code class="EnlighterJSRAW" data-enlighter-language="null">woocommerce_checkout_fields</code>這個filter修改)，預設再幫你驗證一些規則：</p>
<ul>
<li>必填，當required為true時</li>
<li>郵遞區號格式，當validate給入<code class="EnlighterJSRAW" data-enlighter-language="null">postcode</code>時</li>
<li>電話格式：當validate給入<code class="EnlighterJSRAW" data-enlighter-language="null">phone</code>時</li>
<li>Email格式：當validate給入<code class="EnlighterJSRAW" data-enlighter-language="null">email</code>時</li>
<li>縣市(state)有效性：當validate給入<code class="EnlighterJSRAW" data-enlighter-language="null">state</code>時</li>
</ul>
<p>參考<code class="EnlighterJSRAW" data-enlighter-language="php">WC_Checkout::validate_posted_data</code>這個method</p>
<h2>2. 自訂驗證規則</h2>
<p>如果要自訂自己的驗證規則(如身分證字號，手機號碼等)，可使用<code class="EnlighterJSRAW" data-enlighter-language="null">woocommerce_after_checkout_validation</code>這個action hook</p>
<p>以下舉的例子為驗證台灣地區手機號碼的格式：</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="droide">add_action('woocommerce_after_checkout_validation', 'octopuswp_validate_billing_mobile_number');

/**
 * @param array $fields POST進來的結帳欄位資料
 * @param WP_Error $errors
 */
function octopuswp_validate_billing_mobile_number($fields, $errors)
{
    /**
     * 假如手機號碼欄位key值為billing_mobile_number
     * 要驗證台灣手機號碼，規則是09開頭的10碼數字
     */
    if(!preg_match('/^09\d{8}$/', $fields['billing_mobile_number'])) {
        // 這裡傳入的是一個WP_Error的物件參考，只要在驗證失敗時加入一個錯誤就行
        // 第一個參數是自訂的錯誤代碼，第二個參數是錯誤訊息
        $errors-&gt;add('mobile-number-error', '手機號碼格式錯誤，格式為0910123456');
    }
}</pre>
<p>&nbsp;</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/17/woocommerce%e9%a9%97%e8%ad%89%e7%b5%90%e5%b8%b3%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/">WooCommerce驗證結帳欄位的方法</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.octopuswp.com/2020/02/17/woocommerce%e9%a9%97%e8%ad%89%e7%b5%90%e5%b8%b3%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WooCommerce修改結帳欄位的方法</title>
		<link>https://www.octopuswp.com/2020/02/16/woocommerce%e4%bf%ae%e6%94%b9%e7%b5%90%e5%b8%b3%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/</link>
					<comments>https://www.octopuswp.com/2020/02/16/woocommerce%e4%bf%ae%e6%94%b9%e7%b5%90%e5%b8%b3%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/#respond</comments>
		
		<dc:creator><![CDATA[OctopusWP]]></dc:creator>
		<pubDate>Sun, 16 Feb 2020 10:22:16 +0000</pubDate>
				<category><![CDATA[WordPress開發教學]]></category>
		<category><![CDATA[中級]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[技術]]></category>
		<guid isPermaLink="false">https://www.octopuswp.com/?p=1484</guid>

					<description><![CDATA[<p>在WordPress中用程式的方法來修改WooCommerce的結帳欄位(新增自訂欄位或是刪除掉預設欄位) 1 [&#8230;]</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/16/woocommerce%e4%bf%ae%e6%94%b9%e7%b5%90%e5%b8%b3%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/">WooCommerce修改結帳欄位的方法</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></description>
										<content:encoded><![CDATA[<p>在WordPress中用程式的方法來修改WooCommerce的結帳欄位(新增自訂欄位或是刪除掉預設欄位)</p>
<h2>1. 修改結帳欄位</h2>
<p>使用到 <code class="EnlighterJSRAW" data-enlighter-language="null">woocommerce_checkout_fields</code> 這個filter hook，hook上的function會接收到所有結帳欄位的資訊</p>
<p>以下為新增台灣手機號碼帳單欄位為例：</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="droide">&lt;?php
add_filter('woocommerce_checkout_fields', 'octopuswp_add_checkout_mobile_number');
/**
 * 新增台灣手機號碼，移除預設電話欄位
 * @params array $fields
 */
function octopuswp_add_checkout_mobile_number($fields)
{
    // 移除預設電話欄位
    // unset($fields['billing']['billing_phone']);
    /**
     * $fields記錄著所有的結帳欄位資訊
     * 這裡手機號碼欄位的key值使用billing_mobile_number
     */
    $fields['billing']['billing_mobile_number'] = [
        'text'     =&gt; 'text',  //欄位類型
        'label'    =&gt; '手機號碼', //標籤
        'required' =&gt; true, //必填
        'class'    =&gt; ['form-row-wide'] // 全寬欄位的class
    ];
    return $fields;
}
?&gt;</pre>
<p>$field的結構為</p>
<pre class="EnlighterJSRAW" data-enlighter-theme="droide">Array
(
    [billing] =&gt; Array
        (
            [billing_first_name] =&gt; Array
                (
                    [label] =&gt; 名字
                    [required] =&gt; 1
                    [class] =&gt; Array
                        (
                            [0] =&gt; form-row-wide
                        )

                    [autocomplete] =&gt; given-name
                    [priority] =&gt; 0
                )

            [billing_email] =&gt; ...
            [billing_country] =&gt; ...
            [billing_phone] =&gt; ...
        )

    [shipping] =&gt; Array
        (
            [shipping_first_name] =&gt; ...
            [shipping_last_name] =&gt; ...
            [shipping_company] =&gt; ...
            [shipping_country] =&gt; ...
            [shipping_address_1] =&gt; ...
            [shipping_address_2] =&gt; ...
            [shipping_city] =&gt; ...
            [shipping_state] =&gt; ...
            [shipping_postcode] =&gt; ...
        )

    [account] =&gt; Array
        (
            [account_username] =&gt; ...
            [account_password] =&gt; ...

        )

    [order] =&gt; Array
        (
            [order_comments] =&gt; ...

        )

)</pre>
<p>為多層陣列結構，第一層依欄位群組分成</p>
<ul>
<li>billing(帳單資訊)</li>
<li>shipping(運送資訊)</li>
<li>account(帳號資訊)</li>
<li>order(訂單其他資訊)</li>
</ul>
<p>第二層開始紀錄的就是各欄位的詳細資訊了，第二層的key也是最後會寫進資料庫的key名稱，如key為<code class="EnlighterJSRAW" data-enlighter-language="null">billing_first_name</code>，最後會寫進資料庫的meta_key就會是<code class="EnlighterJSRAW" data-enlighter-language="null">_billing_first_name</code></p>
<p>各個欄位的參數有</p>
<ul>
<li>id：html的id，預設為欄位的key值</li>
<li>type：類型，預設為text，可接受的值有<code class="EnlighterJSRAW" data-enlighter-language="null">country、state、textarea、checkbox、text、password、datetime、datetime-local、date、month、time、week、number、email、url、tel、select、radio</code></li>
<li>label：標籤名稱，會顯示在欄位上方</li>
<li>description：補充說明</li>
<li>placeholder：欄位內placeholder</li>
<li>maxlength：html的maxlength屬性值</li>
<li>required：是否必填</li>
<li>autocomplete：html的autocomplete屬性值</li>
<li>validate：驗證格式(陣列)，可接受的值有，<code class="EnlighterJSRAW" data-enlighter-language="null">postcode、email、phone、state</code></li>
<li>class：欄位的容器class(陣列)<br />
在結帳欄位中欄位(input、select)及label會被包上一個p標籤容器，class預設可控制這個欄位的排列方式<br />
控制排列方式的class有<code class="EnlighterJSRAW" data-enlighter-language="null">form-row-wide(全寬)</code>、<code class="EnlighterJSRAW" data-enlighter-language="null">form-row-first(左欄)</code>、<code class="EnlighterJSRAW" data-enlighter-language="null">form-row-last(右欄)</code><br />
也可帶入自訂的class名稱</li>
<li>label_class：label的class(陣列)</li>
<li>input_class：欄位的class(陣列)</li>
<li>options：選項(陣列)，若欄位為select或radio，則須帶入</li>
<li>custom_attributes：其他自訂的html屬性(陣列)</li>
<li>default：預設值，當使用者第一次結帳時的值</li>
<li>autofocus：html的autofocus屬性值</li>
</ul>
<p>欄位資訊最後會透過<code class="EnlighterJSRAW" data-enlighter-language="null">woocommerce_form_field</code>這個function來產生html，詳細用法請參考</p>
<h2>2. 將自訂的結帳欄位顯示在感謝頁面</h2>
<p>感謝頁面的hook有很多，依照想顯示出來的地方挑選一個即可(參考<code class="EnlighterJSRAW" data-enlighter-language="null">woocommerce/tamplates/checkout/thankyou.php</code>)，這裡使用<code class="EnlighterJSRAW" data-enlighter-language="null">woocommerce_before_thankyou</code>這個action hook</p>
<p>範例：</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php">add_action('woocommerce_before_thankyou', 'octopuswp_show_mobile_number_in_thankyou');
/**
 * 將結帳時手機號碼資料顯示在感謝頁面
 * @param int $order_id 訂單ID
 */
function octopuswp_show_mobile_number_in_thankyou($order_id)
{
    $order = wc_get_order($order_id);
    echo '&lt;p&gt;手機號碼：' . $order-&gt;get_meta('_billing_mobile_number') . '&lt;/p&gt;'; // echo出html結構
}</pre>
<p>呈現的效果如下：</p>
<p><img loading="lazy" class="alignnone wp-image-1563" src="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484.png" alt="" width="498" height="171" /></p>
<p>&nbsp;</p>
<p>但這樣看起來怪怪的，因為手機號碼須融入原有感謝頁面的資訊才對，注意到下面有個區塊是顯示帳單資訊的</p>
<p><img loading="lazy" class="alignnone size-full wp-image-1549" src="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-1.png" alt="" width="344" height="299" srcset="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-1.png 344w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-1-300x261.png 300w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-1-265x230.png 265w" sizes="(max-width: 344px) 100vw, 344px" /></p>
<p>經查他是透過<code class="EnlighterJSRAW" data-enlighter-language="php">WC_Order::get_formatted_billing_address</code>這個method產出的html，這是WooCommerce拿來顯示格式化帳單資料html的方法，他會根據不同的國家而有不一樣的顯示方式</p>
<p>而這裡修改的方法有兩種：</p>
<p>第一種是比較遵守遊戲規則，遵照修改格式資料的方式，修改html的顯示格式，讓WooCommerce自己依照格式將資料塞入產出html，範例如下：</p>
<p>參考<code class="EnlighterJSRAW" data-enlighter-language="null">WC_Countries::get_formatted_address</code>，先修改台灣的html格式，格式預設是定義在<code class="EnlighterJSRAW" data-enlighter-language="null">WC_Countries::get_address_formats</code>，最後會經過<code class="EnlighterJSRAW" data-enlighter-language="null">woocommerce_localisation_address_formats</code>這個filter hook</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php">add_filter('woocommerce_localisation_address_formats', 'octopuswp_override_tw_address_format');
/**
 * 修改台灣地址格式
 * @param array $formats
 * @return array mixed
 */
function octopuswp_override_tw_address_format($formats)
{
    $default_tw_address_format = $formats['TW'];
    // 預設的台灣地區地址格式資料，這裡有許多格式字串，WooCommerce會再取代成真正的資料
    // {company}\n{last_name} {first_name}\n{address_1}\n{address_2}\n{state}, {city} {postcode}\n{country}
    // 在這裡把它放在country的後面，所以直接append上去就好，格式字串自訂
    $formats['TW'] = $default_tw_address_format . "\n{mobile_number}";
    return $formats;
}</pre>
<p>接著資料本身是會透過<code class="EnlighterJSRAW" data-enlighter-language="null">WC_Order::get_address('billing')</code>來傳入，透過<code class="EnlighterJSRAW" data-enlighter-language="null">woocommerce_get_order_address</code>這個filter hook來將儲存的手機號碼資料加入</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php">add_filter('woocommerce_get_order_address', 'octopuswp_add_order_address_data', 10, 3);
/**
 * 將手機號碼加入地址資料
 * @param array $data 地址資料
 * @param string $type 帳單或運送
 * @param WC_Order $order
 * @return array
 */
function octopuswp_add_order_address_data($data, $type, $order)
{
    // 只有加入帳單的資料
    if('billing' == $type) {
        $data['mobile_number'] = $order-&gt;get_meta('_billing_mobile_number');
    }
    return $data;
}</pre>
<p>再來就是定義自訂格式字串<code class="EnlighterJSRAW" data-enlighter-language="null">{mobile_number}</code>要被取代成什麼資料</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php">add_filter('woocommerce_formatted_address_replacements', 'octopuswp_add_address_format_replacement', 10, 2);
/**
 * 加入自訂格式字串的陣列取代資料
 * @param array $replacements 要取代的字串對應陣列
 * @param array $data 地址資料
 * @return mixed
 */
function octopuswp_add_address_format_replacement($replacements, $data)
{
    // 因為在這裡只加入帳單(billing)的手機號碼欄位，沒有運送(shipping)的手機欄位
    // 而透過woocommerce_localisation_address_formats修改格式的話沒有辦法去區別是billing還是shipping
    // 且上一步octopuswp_add_order_address_data只加入billing的資料
    // 所以在這裡就要判斷是否有這個資料存在
    $replacements['{mobile_number}'] = !empty($data['mobile_number']) ? $data['mobile_number'] : '';
    return $replacements;
}</pre>
<p>效果如下：</p>
<p><img loading="lazy" class="alignnone wp-image-1548" src="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-2.png" alt="" width="394" height="284" srcset="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-2.png 447w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-2-300x216.png 300w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-2-265x191.png 265w" sizes="(max-width: 394px) 100vw, 394px" /></p>
<p>第二種是直接修改<code class="EnlighterJSRAW" data-enlighter-language="php">WC_Order::get_formatted_billing_address</code>產出的html，透過<code class="EnlighterJSRAW" data-enlighter-language="null">woocommerce_order_get_formatted_billing_address</code>這個filter hook</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php">add_filter('woocommerce_order_get_formatted_billing_address', 'octopuswp_add_mobile_number_to_formatted_address_html', 10, 3);
/**
 * 將自訂欄位資料加入到formatted address的html
 * @param string $formatted_address_html
 * @param array $address_data
 * @param WC_Order $order
 * @return string
 */
function octopuswp_add_mobile_number_to_formatted_address_html($formatted_address_html, $address_data, $order)
{
    $formatted_address_html .= $order-&gt;get_meta('_billing_mobile_number');
    return $formatted_address_html;
}</pre>
<p>&nbsp;</p>
<h2>3. 將自訂的結帳欄位顯示在後台</h2>
<p>後台訂單頁面，預設有個區塊是拿來顯示結帳欄位資訊的</p>
<p><img loading="lazy" class="alignnone wp-image-1555" src="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-3.png" alt="" width="791" height="185" srcset="https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-3.png 1283w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-3-300x70.png 300w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-3-1024x239.png 1024w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-3-768x180.png 768w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-3-265x62.png 265w, https://www.octopuswp.com/wp-content/uploads/2020/02/post-1484-3-504x118.png 504w" sizes="(max-width: 791px) 100vw, 791px" /></p>
<p>可以使用<code class="EnlighterJSRAW" data-enlighter-language="null">woocommerce_admin_billing_fields</code>及<code class="EnlighterJSRAW" data-enlighter-language="null">woocommerce_admin_shipping_fields</code>來將自訂的欄位加進去</p>
<pre class="EnlighterJSRAW" data-enlighter-language="php">add_filter('woocommerce_admin_billing_fields', 'octopuswp_add_admin_billing_fields');
/**
 * 加入自訂欄位資料到後台定單頁面
 * @param array $fields
 */
function octopuswp_add_admin_billing_fields($fields)
{
    // 這裡的key -&gt; mobile_number，會讓woocommerce自己去抓取結帳欄位key為billing_mobile_number的資料
    // 所以只要填入mobile_number就好，不用填入完整的billing_mobile_number
    $fields['mobile_number'] = [
        'label' =&gt; '手機號碼', // 欄位標籤
        'show' =&gt; true //再沒有按下編輯按鈕時，是否顯示出來，預設為true
    ];
    return $fields;
}</pre>
<p>這裡的欄位參數也提供了type、options、class可填，type預設為text</p>
<p>這篇文章 <a rel="nofollow" href="https://www.octopuswp.com/2020/02/16/woocommerce%e4%bf%ae%e6%94%b9%e7%b5%90%e5%b8%b3%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/">WooCommerce修改結帳欄位的方法</a> 最早出現於 <a rel="nofollow" href="https://www.octopuswp.com">OctopusWP | WordPress客製開發專家</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.octopuswp.com/2020/02/16/woocommerce%e4%bf%ae%e6%94%b9%e7%b5%90%e5%b8%b3%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
