ป้ายกำกับ: สลับสี

WordPress: แก้ตารางเก่าให้เป็นแบบ Stripes (แถวสลับสี )WordPress: แก้ตารางเก่าให้เป็นแบบ Stripes (แถวสลับสี )

การเปลี่ยนค่าในฐานข้อมูล WordPress เพื่อเพิ่มหรือแก้ไข Class ให้เป็นแบบ Stripes (แถวสลับสี) ทั้งหมดนั้น สามารถทำได้โดยการใช้คำสั่ง SQL Update ร่วมกับฟังก์ชัน REPLACE


โครงสร้าง

โดยปกติ WordPress จะเก็บรูปแบบตารางใหม่ ๆ โดยจะมีรูปแบบคือ

  • ไม่กำหนด options
    <!– wp:table –>
    <figure><table>
  • ไม่ fixed ความกว้าง column
    <!– wp:table {“hasFixedLayout”:false} –>
    <figure class=”wp-block-table”><table>
  • ไม่ fixed ความกว้าง column และสลับสี
    <!– wp:table {“hasFixedLayout”:false,”className”:”is-style-stripes”} –>
    <figure class=”wp-block-table is-style-stripes”><table>

จะเห็นว่าต่างกันตรง attributes ทำให้เขียน sql update ได้โดยใช้ regular expression ช่วยได้


สำหรับตรวจสอบผลลัพธ์ (SELECT Preview)

ดูผลกระทบก่อนโดยใช้ QUERY ว่าใช้ตามที่ต้องการมั๋ย

SELECT 
 ID,
 post_title,
 CONCAT_WS ('\n',
 REGEXP_SUBSTR (post_content, '<!-- wp:table[^>]*-->') ,
 REGEXP_SUBSTR (post_content, '<figure class="wp-block-table[^"]*">')) AS `original`,
 CONCAT_WS ('\n',
 '<!-- wp:table {"hasFixedLayout":false,"className":"is-style-stripes"} -->',
 REGEXP_REPLACE (REGEXP_SUBSTR (post_content, '<figure class="wp-block-table[^"]*">') ,
 '<figure class="wp-block-table (\\s+align (center|wide|full)) ?[^"]*">',
 '<figure class="wp-block-table\\1 is-style-stripes">')) AS `modify`
FROM wp_posts
WHERE post_content LIKE '%<!-- wp:table%'
 AND post_type IN ('post', 'page') LIMIT 5;

update script

UPDATE wp_posts
SET post_content = REGEXP_REPLACE (REGEXP_REPLACE (post_content, '<!-- wp:table[^>]*-->', '<!-- wp:table {"hasFixedLayout":false,"className":"is-style-stripes"} -->') ,
 '<figure class="wp-block-table (\\s+align (center|wide|full)) ?[^"]*">',
 '<figure class="wp-block-table\\1 is-style-stripes">') WHERE post_content LIKE '%<!-- wp:table%'
 AND post_type IN ('post', 'page') ;

หลังจากนั้น search หาบทความที่มีตารางโดยใช้เงื่อนไข <!-- wp:table แล้วสุ่มดูว่ามันสลับสีและไม่กำหนดความกว้างจริง ๆ มั๋ย


อ่านเพิ่มเติม