WordPress

WordPress Nginx 設定檔

server {
        ## Your website name goes here.
        server_name site.com;
        ## Your only path reference.
        root /var/www/site;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;
        client_max_body_size 100M;
        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi_params;
                fastcgi_intercept_errors on;
                fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
                #The following parameter can be also included in fastcgi_params file
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }

}

fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; 這裡要改成主機實際的版本

多網站的範例

server {
        ## Your website name goes here.
        server_name site.com;
        ## Your only path reference.
        root /var/www/site;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;
client_max_body_size 20M;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }

# Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;



 # Pass uploaded files to wp-includes/ms-files.php.
    rewrite /files/$ /index.php last;

    if ($uri !~ wp-content/plugins) {
        rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
    }

    # Rewrite multisite '.../wp-.*' and '.../*.php'.
    if (!-e $request_filename) {
        rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
        rewrite ^/[_0-9a-zA-Z-]+.*(/wp-admin/.*\.php)$ $1 last;
        rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
    }


        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                include fastcgi_params;
                fastcgi_intercept_errors on;
                #The following parameter can be also included in fastcgi_params file
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
       } 


}

Nginx多網站時必須這樣設定才能讓多網站的路徑能正常運作

使用wp-cli安裝WordPress

安裝wp-cli

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

wp-cli安裝參考:https://wp-cli.org/#installing

新增目錄並使用wp-cli下載最新版本的WordPress主程式

cd /var/www/
mkdir site
cd site
wp core download --locale=zh_TW --allow-root

調整目錄權限

chown -R www-data:www-data /var/www/site

設定SSL

certbot -d site.com

完成後就可以輸入 https://site.com 進站完安裝程序

使用wp-cli維護更新程式

vi wpnew.sh
#!/bin/bash

# 充許root使用WP CLI
export WP_CLI_ALLOW_ROOT=1

# 列出要更新的網站
SITES=("site" "site1" "site2" )

# 更新網站語法
  for SITE in "${SITES[@]}"; do
  SITE_DIR="/var/www/$SITE"
  cd "$SITE_DIR"

  # 執行更新動作
  wp core update
  wp plugin update --all
  wp core language update
  wp core update-db
  wp theme update --all
  wp db optimize

  echo "網站 $SITE 更新完成"
done
chomd +x wpnew.sh

之後執行 ./wpnew.sh 即可更新網站以及最佳化資料庫

安裝WordPress後的基本設定

WordPress裝好後務必立即啟用防火牆相關的外掛,並更換wp-admin預設路徑

防火牆及安全相關外掛我目前使用:All In One WP Security、Falcon

接著就是一些基本設定,我是習慣再做這些設定
01.永久連結設定選擇文章名稱
02.媒體設定為上傳的檔案建立以年份及月份命名的資料夾勾選取消

其他我會預設安裝外掛
安全相關:All In One WP Security、Falcon
效能相關:APCu Manager、DecaLog、Cache Enabler
SEO相關:Slim SEO、Super Socializer、XML Sitemap & Google News
圖片管理的部份,由於我是用圖床,採用 External Media without Import 處理圖的問題

WooCommerce

WooCommerce

設定

初始設定

設定精靈先全部略過,僅需填寫地區 台灣,並且先不要註冊及開通連結wordpress.com相關的任何功能

使用外掛

Advanced Order Export For WooCommerce - 匯出訂單
Checkout Field Editor for WooCommerce - 客制結帳表單
WooCommerce - 購物站主程式
WooCommerce Google Analytics Integration - Google分析器(建議直接裝這個版本才有辦法分析到商務資訊)
WooCommerce Advanced Free Shipping - 處理免運費,可搭配 RY WC City Select 做出外島費用不同的功能
RY WC City Select - 地址選擇器

佈景主題

Blocksy

我主要用這幾個外掛就能把購物站做出來,客制結帳表單的部份,主要是把姓名只保留一格成"姓名",這樣較容易處理,然後收件人再增加聯絡電話欄位,地址一堆欄位也有留一格"地址"即可

一頁式網站

新增一個頁面,插入shortcode
商品 + [woocommerce_cart] + [woocommerce_checkout]

WooCommerce

雜記


我還蠻建議一開始不要把網站弄的太複雜,讓他只能接單就好,等網站到一定的規模再來規劃下一步
例如月收100萬,月收到200萬,再來找合適的平台去處理,這裡所謂合適的平台,就是可以在線上把金流、物流、發票等事情,直接在線上解決掉,等後面有賺到錢以及你的理貨方式順暢後,再去思考及規劃

基本上你的營業額及獲利沒達到一個水準,坦白說我不建議你花錢去客製站,客製站也是有開發、維護的成本,而且會比租用的還貴

就行銷層面來說,如果你已經有辦法與客人之間的銷售溝通不用使用折扣,點數等銷售手法進行銷售,那麼你的品牌價值其實就已經有了,不用和客人在價格上面高來高去,蠻多老闆都喜歡說要跟Apple或Lv等等品牌去比,光商品上架的第一步,有定價跟售價就已經是錯了

WooCommerce

外掛技巧

Advanced Order Export For WooCommerce 

使用前可先參考官方的介紹案例 :https://docs.algolplus.com/algol_order_export/hooks-filters/

訂單編號前面加英文GG

add_filter('woe_get_order_value_custom_num',function ($value, $order,$fieldname) {
$value = 'GG' .$order->get_order_number();
return $value;
},10,3);

溫層的欄位值修改,客制欄位 additional_temp,以宅急便溫層為案例

add_filter('woe_get_order_value_custom_temp', function ($value, $order, $fieldname) {
    $additional_temp_value = get_post_meta($order->get_id(), 'additional_temp', true);

    if ($additional_temp_value === '冷凍') {
        return '0';
    } else {
        return '1';
    }
}, 10, 3);

add_filter('woe_get_order_value_custom_additional_receitp_id', function ($value, $order, $fieldname) {
    $additional_receitp_id_value = get_post_meta($order->get_id(), 'additional_receitp_id', true);

    if (empty($additional_receitp_id_value)) {
        return '1';
    } else {
        return '0';
    }
}, 10, 3);

 

WooCommerce

function

圖片管理

以原始檔上傳圖片(圖片只會保留1張,預設會壓成6張)

function disable_image_resize() {
    add_filter('jpeg_quality', function($arg){return 100;});
    add_filter('wp_editor_set_quality', function($arg){return 100;});
    add_filter('big_image_size_threshold', '__return_false');
    add_filter('intermediate_image_sizes_advanced', function($sizes){return array();});
}
add_action('init', 'disable_image_resize');

我是習慣先壓再傳 https://squoosh.app/

客制目錄圖片

可以主圖片與目錄圖片想要用不同張呈現,可使用這個function

function change_product_thumbnail($thumbnail_id, $product_id) {
  $product = wc_get_product($product_id);
  if ($product) {
    $image_gallery = $product->get_gallery_image_ids();
    if (!empty($image_gallery)) {
      $thumbnail_id = $image_gallery[0];
    }
  }
  return $thumbnail_id;
}
add_filter('woocommerce_product_get_image_id', 'change_product_thumbnail', 10, 2);
add_filter('woocommerce_product_variation_get_image_id', 'change_product_thumbnail', 10, 2);

function 管理可使用 Code Snippets

WordPress換網域且人工搬站的作法

換網域後需要執行路徑轉換,可透過phpMyAdmin進行搜尋及修改
 
UPDATE wp_options
SET option_value = REPLACE(option_value, 'oldsite.com', 'newsite.com')

UPDATE wp_postmeta
SET meta_value = REPLACE(meta_value, 'oldsite.com', 'newsite.com');

UPDATE wp_posts
SET post_content = REPLACE(post_content, 'oldsite.com', 'newsite.com');
其他的漏網之魚,再用 phpMyAdmin 慢慢找,再進行取代

搬站的正規作法則使用 WPvivid 或 UpdraftPlus,除非你用這兩套升失敗,否則的話用Plugins搬即可,搬完注意路徑有沒有正常或漏圖漏檔

WordPress最佳還原方法

備份後的資料要如何還原,大家應該蠻苦腦的,我在還原公司資料量較大的網站時,測試出UpdraftPlus最佳以及最穩定的還原方法,提供大家做參考

查詢原站的WordPress版本,然後進行降級

到網站狀態資訊取得原站版次

我查到的是6.0.1

安裝降版次外掛

安裝並啟用 WP Downgrade
啟用後要到更新頁面執行重裝 6.0.1

安裝備份程式UpdraftPlus

原站安並啟用 Download Plugin
接著在外掛目錄下載 UpdraftPlus,即可得到和原站同版次的程式

執行並下載備份的資料庫

到 UpdraftPlus Backup/Restore 頁面執行備份(我們只需備份資料庫就好,檔案不用),備份後下載即可,

複製目錄

wp-content/plugins
wp-content/themes
wp-content/uploads
不管用什麼方式,把原站的這三個目錄複製到新站的目錄中

還原資料庫

確認新站的內容都複製好了,且WordPress版次也都相同後,就可以開始還原
到 UpdraftPlus Backup/Restore 頁面,點擊 Upload backup files ,上傳原站備份的資料庫,上傳後進行還原,順利的話不用等幾分鐘就能成功

為什麼要那麼搞剛,不是有備份目錄的功能為何不用?我在進行了好幾次的備份還原動作時,發現會漏圖,而且如果備份檔如果有包含目錄又會跑超慢,還原成功的機率也比較低,以上提供參考

WordPress的資料內容大多使用絕對路徑,例如
https://yoursite.com/123.jpg
因此搬站的時候,也要調整路徑的名稱,如果你是A站換成B站,且網域有改變,