PHP

How Smart Tools Are Shaping the Future of Creative Work

Eric Chuang
2020-10-10

如果你找到網路上關於短代碼 shortcodes 的 WordPress 教學,有時會發現以下的範例:

add_shortcode( 'my_loop', 'hyc_shortcode' );
function hyc_shortcode( $atts ) {
  extract( shortcode_atts( array (
    'post_type'      => 'post',
    'posts_per_page' => 10,
  ), $atts ) );
}

$args = array(
  'post_type'      => $post_type,
  'posts_per_page' => $posts_per_page,
);

但根據 WordPress 的 PHP 程式碼撰寫標準,這其實是不建議的作法。

extract() 在做什麼

根據 PHP 的說明文件,extract() 會將陣列中的索引鍵與索引鍵值取出,以索引鍵為名稱去定義變數。譬如說:

$arr = array(
  'user_id'    => 0,
  'user_name'  => 'Eric',
);

extract( $arr );

echo $user_id; // 回傳 0
echo $user_name; // 回傳 Eric

然而,你並不知道這些變數是否已經在全域變數中定義過,也不知道是否會觸發到未預期的結果,因此在 PHP 的說明文件中,也特別警告:

如有尚未信任的資料來源,例如 $_GET、$_FILE 或其他使用者輸入值。不要使用 extract()。如果你使用 extract(),必須搭配對應標誌 (flag)。

也因為有許多未預期的結果,會造成偵錯流程困難,以及程式可讀性減低,因此在「編碼標準」中,便明確禁止這個做法。但其實可以看到,許多外掛開發者都還是使用這種作法。

正確的作法

為了提高程式碼的可讀性,因此正確的作法,是將短代碼的參數另外儲存至一個陣列中。我們將上述的代碼改寫:

add_shortcode( 'my_loop', 'hyc_shortcode' );
function hyc_shortcode( $atts ) {
  $a = shortcode_atts( array (
    'post_type'      => 'post',
    'posts_per_page' => 10,
  ), $atts );
}

$args = array(
  'post_type'      => $a['post_type'],
  'posts_per_page' => $a['posts_per_page'],
);

結語

今天這篇文章,主要是說明為什麼不該使用 extract() 這個方法。關於更多 shortcode 的實際作法,可以參考我在鐵人賽的文章,裡面提到如何透過短代碼的機制,來擴展既有短代碼的功能。

參考資料

Sign up for our newsletter

Subscribe to our newsletter to receive the latest updates, expert insights, and practical tips directly in your inbox. Stay informed with content designed to help you grow.

+480.000 happy customers

Gain more with expert solutions

Unlock the full potential of your business with our expert solutions. Our team provides tailored strategies, professional guidance, and actionable insights to help you achieve better results.

  • Eric Chuang
  • •
  • 2025-01-23
如何同時使用 Webinoly 與 Cloudflare 保護網站安全

在社群夥伴勇哥的文章〈Webinoly 最新版快速安裝〉中,提到使用 Webinoly 來建立 WordPre […]

主機