Cách sử dụng Action Hook trong WordPress

57 sec read

action hook trong wordpress

Để có thể trở thành deverloper thực thụ về WordPress thì bạn sẽ phải biết cách sử dụng hook (add_action(), add_filter()), nó là thứ cốt lõi trong WordPress.
Hook cũng được sử dụng trong Core của WP. WordPress có 2 kiểu Hook chính: Action hook và Filter hook. Trong bài hôm nay chúng ta sẽ tìm hiểu về Action hook

Action hook là gì?

Đặc tính : Không làm thay đổi giá trị

add_action('wp_footer', 'ncc_myplugin_footer');
function ncc_myplugin_footer() {
   echo '<div>Chao mung ban den voi footer</div>';
}

Cách sử dụng: (Reference: https://developer.wordpress.org/reference/functions/add_action/)

add_action($tag, $function, $priority, $accepted_args)
Ý nghĩa của các tham số:
$tag: Là tên của một action hook của WP (Wp có hơn 600 hooks).
$function: Là tên Action mà mình viết để tương tác vào hệ thống WP
$priority: Độ ưu tiên của Action.
$accepted_args: Số lượng các tham số action hook sẽ được truyền vào hàm.

Những kiến thức khác

– Để có thể xóa action ta dùng :

remove_action( $tag, $function_to_remove, $priority, $accepted_args );

– Để loại bỏ tất cả

remove_all_actions( $tag, $priority );

-Để kiểm tra sự tồn tại

has_action( $tag, $function_to_check );

Avatar photo

𝐌𝐄𝐙𝐎𝐍 – 𝐓𝐇𝐄 𝐔𝐋𝐓𝐈𝐌𝐀𝐓𝐄 𝐏𝐋𝐀𝐓𝐅𝐎𝐑𝐌 𝐅𝐎𝐑 𝐂𝐎𝐌𝐌𝐔𝐍𝐈𝐓𝐈𝐄𝐒 & 𝐁𝐔𝐒𝐈𝐍𝐄𝐒𝐒

Discord is a widely used communication platform, but is it truly the best solution for community and business management? While it excels in creating...
Avatar photo Ngan Ton Thuy
3 min read

𝐌𝐞𝐳𝐨𝐧 𝐯𝐬 𝐒𝐥𝐚𝐜𝐤 – 𝐖𝐡𝐢𝐜𝐡 𝐎𝐧𝐞 𝐄𝐧𝐡𝐚𝐧𝐜𝐞𝐬 𝐘𝐨𝐮𝐫 𝐁𝐮𝐬𝐢𝐧𝐞𝐬𝐬…

Both Mezon and Slack are powerful communication platforms, but which one is the right fit for your organization? Let’s compare their key features: 𝐔𝐬𝐚𝐠𝐞...
Avatar photo Ngan Ton Thuy
1 min read

𝐌𝐄𝐙𝐎𝐍 𝐂𝐀𝐌𝐏𝐔𝐒 𝐏𝐀𝐑𝐓𝐍𝐄𝐑 — 𝐄𝐦𝐩𝐨𝐰𝐞𝐫𝐢𝐧𝐠 𝐒𝐭𝐮𝐝𝐞𝐧𝐭-𝐋𝐞𝐝 𝐈𝐧𝐢𝐭𝐢𝐚𝐭𝐢𝐯

Mezon Campus Partner is an all-in-one program designed to support: – Student competitions – Career development initiatives – Student-led clubs and creative teams 𝐖𝐡𝐚𝐭...
Avatar photo Ngan Ton Thuy
44 sec read

One Reply to “Cách sử dụng Action Hook trong WordPress”

Leave a Reply

Your email address will not be published. Required fields are marked *