สอนสร้าง Plugin WordPress (3) : ทำความรู้จัก Action Hook เขียนโค้ดให้ทำงานตามสาเหตุการที่เกิดขึ้น

แชร์ ความรู้สำหรับพัฒนา plugin ของ Wordpress

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

สอนสร้าง Plugin WordPress (3) : ทำความรู้จัก Action Hook เขียนโค้ดให้ทำงานตามสาเหตุการที่เกิดขึ้น

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

จากบทความที่เเล้วเราได้ทำความรู้จัก Hooks (ฮุค) WordPress ไปเเล้ว ในส่วนของบทความนี้จะขออธิบาย ความเข้าใจเกี่ยวกับ
  • Action

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

add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 ) 
Action คือการเขียน Code คำสั่งการทำงาน สำหรับกำหนดให้ทำงานเมื่อเกิดเหตุการณ์นั้นๆ เช่น Hook เข้าไปใน save_post เพื่อส่งอีเมลเตือน admin หรือจะเป็นการสั่งให้สร้าง Menu Plugin หลังจากกด Activate เป็นต้น


$tag (string) (Required)
  • ชื่อ function หรือ ชื่อเหตุการณ์ที่จะเกิด เช่น save_post หมายความว่า เมื่อมีการกด Save post เราจะเขียนคำสั่งเพิ่มที่นี่ ...
    , admin_menu หมายความว่า ต้องการใช้เพิ่ม Menu หน้า Admin ชื่อ ...
    ศึกษารายละเอียด $tag ได้ที่ https://codex.wordpress.org/Plugin_API/Action_Reference
$function_to_add (callable) (Required)
  • ชื่อของ function ที่เราจะเขียนเพื่อสั่งให้ทำงาน ตอนเหตุการณ์ที่เรา ระบุไว้เกิดขึ้น


ตัวอย่าง
การเพิ่ม Menu ในหน้า Admin

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

/**
 * Register a custom menu page.
 */
function wpdocs_register_my_custom_menu_page(){
    add_menu_page( 
        __( 'Custom Menu Title', 'textdomain' ),
        'custom menu',
        'manage_options',
        'custompage',
        'my_custom_menu_page',
        '',//plugins_url( 'myplugin/images/icon.png' ),
        1
    ); 
}
add_action( 'admin_menu', 'wpdocs_register_my_custom_menu_page' );
 
/**
 * Display a custom menu page
 */
function my_custom_menu_page(){
    esc_html_e( 'Admin Page Test', 'textdomain' );  
}

 
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41120
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: สอนสร้าง Plugin WordPress (3) : ทำความรู้จัก Action Hook

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

รวมกระทู้ บทความสอนสร้าง Plugin WordPress
https://www.mindphp.com/forums/viewtopic ... 25&t=36079
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 23