<?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>初級 &#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/%E5%88%9D%E7%B4%9A/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>初級 &#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>
	</channel>
</rss>
