WP63
  • Web Development
  • WordPress Development
  • Plugins
  • WP63
  • Gutenberg

สร้างบล็อค Gutenberg แบบง่ายๆ ด้วย Advanced Custom Fields

Published February 11, 2019

สร้างบล็อค Gutenberg แบบง่ายๆ ด้วย Advanced Custom Fields
Share this:
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on LINE (Opens in new window)

หลังจากที่เวิร์ดเพรส 5.0 ได้นำเสนอ Gutenberg ซึ่งเป็นตัวแก้ไขเนื้อหาตัวใหม่ที่เขียนด้วยจาวาสคริปท์ออกมา ก็ได้มีเครื่องมือหลายตัวถูกปล่อยออกมาเพื่อช่วยให้เราสามารถสร้างบล็อค Gutenberg เพิ่มเติมได้ง่ายขึ้นเช่น create-guten-block ที่เราเคยพูดถึงไปบ้างแล้ว

สำหรับใครที่ทำเว็บใหญ่ๆ ด้วยเวิร์ดเพรสมาก่อน คงคุ้นเคยกับปลั๊กอินคู่บุญชาวเวิร์ดเพรสอย่าง Advanced Custom Fields เป็นอย่างดี ซึ่งตัวมันนั้นครอบคลุมสากเบือยันเรือรบ เช่นทำ Custom field ให้กับ Post, Page, Category, User, ไปจนถึงฟิลด์ของ Widget, และหน้า Option ของธีม

และแน่นอนว่า Advanced Custom Fields ก็ไม่พลาดที่จะโดดมาร่วมวงกับ Gutenberg ด้วย และได้เพิ่มฟีเจอร์รองรับการสร้างบล็อค Gutenberg เข้ามาใน ACF Pro 5.8

ในขณะที่เขียนบทความนี้อยู่ ACF รุ่นเสถียรล่าสุดจะอยู่ที่รุ่น 5.7.11 ส่วน ACF 5.8 ยังอยู่ใน Beta 3 ซึ่งผู้ใช้จำเป็นต้องซื้อ ACF Pro เอาไว้ก่อน จึงจะสามารถเข้าไปโหลด ACF 5.8 Beta ได้

โดยปกตินั้นการที่เราจะสร้างบล็อค Gutenberg ได้ เราจำเป็นต้องมีความรู้เรื่อง JavaScript ก่อน และจะต้องมีพื้นฐานของ React อยู่ในระดับหนึ่ง แต่ฟีเจอร์การสร้างบล็อค Gutenberg ใน ACF นี้จะทำให้เราไม่ต้องยุ่งกับการเขียน JavaScript เลยแม้แต่ตัวเดียว

ในการสร้างบล็อคด้วย ACF นั้นมีเพียง 4 ขั้นตอนเท่านั้นคือ

  1. ประกาศฟังก์ชันสำหรับรีจิสเตอร์บล็อคใหม่
  2. ประกาศฟังก์ชันสำหรับใช้เรนเดอร์บล็อค
  3. ฮุคฟังก์ชันรีจิสเตอร์บล็อคเข้าไปในแอคชัน acf/init
  4. สร้างฟิลด์ ACF ที่ลิงก์กับบล็อคของเรา

เราไปเริ่มกันเลยดีกว่า

รีจิสเตอร์บล็อกใหม่

ขั้นแรกให้เราสร้างไฟล์เปล่าๆ ขึ้นมาหนึ่งไฟล์ จากนั้นประกาศคลาสด้วยชื่อที่เราต้องการ (จะใช้ namespace หรือไม่ก็ได้ แล้วแต่สะดวกเลย แต่เราแนะนำให้ใช้)

<?php
namespace WP63\Block;

class SampleBlock {

}

จริงๆ เราสามารถสร้างบล็อคใหม่นี้ด้วยการเขียนแบบ procedural ทั้งหมดได้ แต่ในบทความนี้เราจะพาไปเขียนแบบ OOP เพราะเมื่อมีบล็อคจำนวนมากขึ้น การเขียนแบบ OOP จะเป็นระเบียบกว่านิดหน่อย

ขั้นตอนต่อไปให้เราประกาศเมท็อด __invoke() ขึ้นมาภายในคลาส เพื่อที่จะใช้เป็นเมท็อดสำหรับรีจิสเตอร์บล็อคของเรา

เมท็อด __invoke() เป็นเมจิกเมท็อดของ PHP ที่จะทำงานเมื่อคลาสถูกเรียกขึ้นมาในฐานะ callable (เช่นผ่าน call_user_func())

ภายในเมท็อดนี้จะมีการเรียกใช้ฟังก์ชัน acf_register_block() สำหรับรีจิสเตอร์บล็อคใหม่

<?php
namespace WP63\Block;

class SampleBlock {
  public function __invoke() {
    acf_register_block([
      'name' => 'wp63_sample',
      'title' => __('Sample Block', 'textdomain'),
      'description' => __('Sample block for Gutenberg', 'textdomain'),
      'render_callback' => [$this, 'render'],
      'category' => 'layout',
      'icon' => 'editor-table'
    ]);
  }
}

ฟังก์ชัน acf_register_block() จะรับอาร์กิวเมนต์เป็นอาเรย์ โดยจะมีค่าต่างๆ ดังนี้

  • name ชื่อของบล็อค (unique ห้ามซ้ำ)
  • title ชื่อที่จะใช้แสดงในหน้า Gutenberg
  • description คำอธิบายบล็อค
  • render_callback ฟังก์ชันสำหรับเรนเดอร์บล็อค
  • category หมวดหมู่ของบล็อค โดยค่าเริ่มต้นจะมี 5 หมวดคือ common, formatting, layout, widgets, และ embed
  • icon ไอคอนของบล็อค ระบุเป็นชื่อ dashicon

ตรงนี้อยากให้สังเกตุ 2 อย่างคือ อย่างแรกที่ title และ description จะมีการครอบฟังก์ชัน __() เอาไว้ ฟังก์ชันนี้จะเป็นฟังก์ชันทำให้ข้อความในฟังก์ชันสามารถ “แปล” ได้ ส่วนพารามิเตอร์ที่สอง (ที่เขียนไว้ว่า textdomain) ตรงนี้คือ text domain ที่โดยปกติจะใช้จัดกลุ่มว่าข้อความนี้เป็นของธีมหรือปลั๊กอินไหน

และอย่างที่สองคือที่อาร์กิวเมนต์ render_callback ที่แทนที่เราจะใส่ชื่อฟังก์ชันลงไปตามปกติ แต่เราใส่อาเรย์ลงไปแทน การใส่อาเรย์ลงไปแบบนี้จะเป็นการเรียกใช้เมท็อดที่อยู่ภายในคลาสมาเป็น callable function แทนการเรียกฟังก์ชันปกติแทน โดนอาเรย์ค่าแรกคือชื่อคลาส ($this จะหมายถึง instace ของคลาสปัจจุบัน) และอาเรย์ตัวที่สองจะเป็นชื่อเมท็อดที่ต้องการเรียก

เมท็อดสำหรับเรนเดอร์บล็อค

หลังจากเรารีจิสเตอร์บล็อคผ่านเมท็อด __invoke() เรียบร้อยแล้ว ขั้นต่อไปคือสร้างเมท็อดสำหรับใช้เรนเดอร์บล็อคของเรา ซึ่งจากในขั้นตอนที่แล้ว เราได้ระบุเมท็อดที่ชื่อว่า render() เอาไว้ในอาร์กิวเมนต์ render_callback ดังนั้นก็ให้เราประกาศเมท็อดนี้ขึ้นมาภายในคลาส โดยเมท็อดนี้จะรับพารามิเตอร์หนึ่งตัวคือ $block ที่เป็นตัวแปรเก็บค่าการตั้งค่าต่างๆ ของบล็อคเอาไว้

<?php
namespace WP63\Block;

class SampleBlock {
  public function __invoke() {
    acf_register_block([
      'name' => 'wp63_sample',
      'title' => __('Sample Block', 'textdomain'),
      'description' => __('Sample block for Gutenberg', 'textdomain'),
      'render_callback' => [$this, 'render'],
      'category' => 'layout',
      'icon' => 'editor-table'
    ]);
  }

  public function render( $block ) {
    echo '<section class="wp63-samplecard">';
    echo '<h3 class="title">' . get_field('title') . '</h3>';
    echo '<div class="description">' . get_field('description') . '</div>';
    echo '</section>';
  }
}

ภายในเมท็อด render() จะไม่มีอะไรพิเศษเลยนอกจากการแสดงผล เราสามารถแสดงผล html และเรียกใช้ฟิลด์ ACF ผ่านฟังก์ชัน get_field() (และอื่นๆ) ได้ตามปกติเหมือนกับที่เราใช้งานในธีมของเรา และสามารถเรียกใช้การตั้งค่าของบล็อคได้จากตัวแปร $block ด้วย

อีกวิธีหนึ่งที่จะทำให้โค้ดส่วนนี้สะอาดขึ้นคือแยกส่วนแสดงผลไปใส่ไว้ในไฟล์เท็มเพลตแยกกันเลย แล้วใช้วิธี include() ไฟล์เท็มเพลตนั้นเข้ามาแสดงผล

  public function render( $block ) {
    if( file_exists( get_theme_file_path("/template-parts/block/sample-block.php") ) ) {
        include( get_theme_file_path("/template-parts/block/sample-block.php") );
    }
  }
<section class="wp63-samplecard">
  <h3 class="title"><?php the_field('title'); ?></h3>
  <div class="description"><?php the_field('description'); ?></div>
</section>

ส่วนไฟล์พวก CSS และ JavaScript ต่างๆ ที่จำเป็นต่อการแสดงผล เราสามารถ enqueue เข้ามาตามปกติได้เลย

ฮุคคลาสเข้ากับแอคชัน acf/init

ขั้นตอนสุดท้ายที่จะได้ยุ่งเกี่ยวกับการเขียนโค้ดนี้คือการฮุคคลาสของเราเข้ากับแอคชัน acf/init เพื่อให้ทำการรีจิสเตอร์บล็อคของเราเข้าไปในระบบในช่วงที่ ACF เริ่มการทำงานเลย

ให้เราเปิดไฟล์ functions.php ของธีมเราขึ้นมา (หรือไฟล์อื่นๆ แล้วแต่ที่เราออกแบบระบบไว้) ให้ทำการ include ไฟล์คลาสของเราเข้ามาให้เรียบร้อย (หรือใครจะใช้ autoload ก็ได้) แล้วก็ฮุคเข้าไปใน acf/init ตามนี้

<?php
require_once('gutenberg-blocks/sample.php');

add_action('acf/init', new WP63\Block\SampleBlock);

เนื่องจากเราเขียนบล็อคของเราเป็นคลาส ดังนั้นแทนที่เราจะระบุ callback function เป็นชื่อฟังก์ชันตามปกติ ก็ให้เราสั่ง new เพื่อสร้าง instance คลาสเราขึ้นมา (จากตัวอย่างจะเป็นการใช้คลาสแบบมีกำหนด namespace ถ้าเราไม่ได้ใช้ namespace ก็สามารถ new ClassName ได้เลย)

ถึงตรงนี้แล้วบล็อคของเราก็จะปรากฎขึ้นมาใน Gutenberg แล้ว เพียงแต่หากกดไปแล้วก็ยังไม่มีอะไรเกิดขึ้น เพราะเรายังไม่ได้สร้างฟิลด์ใดๆ ให้กับบล็อคนี้

สร้างฟิลด์ใน ACF

หลังจากเรารีจิสเตอร์บล็อคเสร็จแล้ว ให้เราสร้างฟิลด์กรุ๊ปใหม่ขึ้นมาโดยไปที่ Custom Fields > Add New และในส่วน Location ให้เราเลือกบล็อคที่เราเพิ่งรีจิสเตอร์เข้ามา

จากนั้นก็ให้เราสร้างฟิลด์ขึ้นมาตามปกติเหมือนกับที่เราสร้างฟิลด์ให้เท็มเพลตต่างๆ

การใช้งานบล็อคที่สร้างด้วย ACF

บล็อคที่สร้างด้วย ACF จะมีการแสดงผลอยู่ 2 โหมด นั่นคือโหมด Preview ที่จะแสดงผลบล็อคของเราจริงๆ ว่าจะหน้าตาเป็นอย่างไร โดยในโหมดนี้เราจะสามารถแก้ไขบล็อคได้ผ่าน sidebar ของ Gutenberg และอีกโหมดคือโหมด Edit ที่จะขึ้นฟิลด์ ACF มาให้เราแก้ไขได้ตามปกติบนตัวบล็อคโดยตรง

  • โหมดพรีวิว
  • โหมดแก้ไข

เมื่อเราแก้ไขค่าใดๆ แล้ว บล็อคก็จะอัปเดตการแสดงผลให้อัตโนมัติด้วย

ทิ้งท้าย

เราเคยบอกไปหลายรอบแล้วว่าในที่สุดแล้วยังไงเราก็ไม่มีทางหนี Gutenberg พ้น ดังนั้นการหัดมันเสียตั้งแต่เนิ่นๆ จะทำให้เรายิ่งได้เปรียบคนอื่น ทั้งนี้การเขียน ES6 ยังเป็นกำแพงสำคัญของคนทำเวิร์ดเพรส (ที่ว่ากันตรงๆ ว่าส่วนใหญ่ก็ไม่ได้ศึกษาเรื่องนี้ไว้สักเท่าไหร่) ดังนั้นเครื่องมือจำนวนมากจึงถูกสร้างออกมาเพื่อให้คนทำเวิร์ดเพรสสามารถหันไปใช้ Gutenberg ได้ง่ายขึ้น

คือเครื่องมือที่ว่านี่มีแม้กระทั่งกดลากๆ สร้างบล็อคจากหลังบ้านด้วยซ้ำไป แต่เราไม่แนะนำเท่าไหร่ เพราะดูท่าแล้วน่าจะหนักน่าดู

ส่วนทางของ ACF นั้นเรียกได้ว่าเป็นการปรับตัวหนีตายที่น่าประทับใจมากๆ เพราะสามารถเชื่อมจุดแข็งของตัวเองเข้ากับจุดอ่อนของ Gutenberg ได้เป็นอย่างดี ทั้งนี้อย่างไรก็ตาม ACF 5.8 ยังคงอยู่ในสถานะ Beta ดังนั้นในอนาคตฟีเจอร์เกี่ยวกับการสร้างบล็อคอาจจะยังมีการเปลี่ยนแปลงได้อีก ซึ่งเป็นอย่างไรเราจะมาอัปเดตให้ได้อ่านกันเรื่อยๆ ครับ

แหล่งอ้างอิง – ACF 5.8 – Introducing ACF Blocks for Gutenberg

Share this:
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on LINE (Opens in new window)

บทความอื่นๆ ที่น่าสนใจ

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2022 WP63

  • หน้าแรก
  • ติดต่อเรา
  • นโยบายความเป็นส่วนตัว
  • ข้อตกลงการใช้งาน
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
  • Web Development
  • WordPress Development
  • Plugins

Like us

Like us

Categories

  • Blog
  • Gutenberg
  • Plugins
  • Shortnotes
  • Snippets
  • Web Development
  • WordPress Development

Popular Posts

  • ส่งอีเมลด้วย SendGrid
    ส่งอีเมลด้วย SendGrid
  • array_map() ใน PHP
    array_map() ใน PHP
  • กำหนด URL structure ได้ตามใจด้วย AltoRouter
    กำหนด URL structure ได้ตามใจด้วย AltoRouter
  • การใช้ @media print ในการกำหนด CSS สำหรับพิมพ์และ PDF
    การใช้ @media print ในการกำหนด CSS สำหรับพิมพ์และ PDF
  • การทำ Routing ใน PHP ด้วย AltoRouter
    การทำ Routing ใน PHP ด้วย AltoRouter
  • ทำเว็บให้รองรับ Dark mode ของระบบปฏิบัติการ
    ทำเว็บให้รองรับ Dark mode ของระบบปฏิบัติการ

Archives

  • April 2022
  • January 2021
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • December 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • March 2019
  • February 2019
  • December 2018
  • September 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018