สอนสร้าง Plugin WordPress (16) : ตัวอย่างการสร้าง Custom Meta Boxes

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: สอนสร้าง Plugin WordPress (16) : ตัวอย่างการสร้าง Custom Meta Boxes

Re: สอนสร้าง Plugin WordPress (16) : ตัวอย่างการสร้าง Custom Meta Boxes

โดย mindphp » 25/10/2016 9:47 pm

รวมกระทู้ บทความสอนสร้าง Plugin WordPress
https://www.mindphp.com/forums/viewtopic ... 25&t=36079

สอนสร้าง Plugin WordPress (16) : ตัวอย่างการสร้าง Custom Meta Boxes

โดย thatsawan » 25/10/2016 12:02 pm

Custom Meta Boxes
  • พูดให้เข้าใจๆ ง่ายๆ มันก็คือการเพิ่ม Box ของฟิลด์ใน Post type ต่างๆ โดยสามารถจะระบุได้ว่าจะว่าง box นี้ที่ตำแหน่งใหน

โค้ด: เลือกทั้งหมด

add_meta_box( string $id, string $title, callable $callback, string|array|WP_Screen $screen = null, string $context = 'advanced', string $priority = 'default', array $callback_args = null ) 
ตัวอย่างการใช้งาน

โค้ด: เลือกทั้งหมด

function custom_meta_box_markup()
{
    echo 'hello';
}

function add_custom_meta_box()
{
    add_meta_box("demo-meta-box", "Custom Meta Box", "custom_meta_box_markup", "post", "side", "high", null);
}

add_action("add_meta_boxes", "add_custom_meta_box"); 
*"post", post type ที่ต้องการ Add
* "side", ตำแหน่งที่ต้องการ
* "high" ระดับในหน้าเเสดงผล

ผลที่ได้
รูปภาพ



ตัวอย่างการใช้งาน

โค้ด: เลือกทั้งหมด

function custom_meta_box_markup()
{
    echo '<label for="my_meta_box_text">Text Label</label>
    <input type="text" name="my_meta_box_text" id="my_meta_box_text" />';
}

function add_custom_meta_box()
{
    add_meta_box("demo-meta-box", "Custom Meta Box", "custom_meta_box_markup", "post", "side", "high", null);
}

add_action("add_meta_boxes", "add_custom_meta_box"); 
ผลที่ได้
รูปภาพ

ศึกษาเพิ่มเติมที่นี่
https://developer.wordpress.org/referen ... _meta_box/

ข้างบน