
{"id":9848,"date":"2025-05-09T13:16:42","date_gmt":"2025-05-09T06:16:42","guid":{"rendered":"https:\/\/tapchicntt.com\/?p=9848"},"modified":"2025-05-09T13:16:42","modified_gmt":"2025-05-09T06:16:42","slug":"wordpress-huong-dan-tao-custom-action-hook-trong-wordpress","status":"publish","type":"post","link":"https:\/\/tapchicntt.com\/wordpress-huong-dan-tao-custom-action-hook-trong-wordpress\/","title":{"rendered":"[WordPress] H\u01b0\u1edbng d\u1eabn t\u1ea1o Custom Action Hook trong WordPress"},"content":{"rendered":"\n<p>T\u1ea1o custom <strong>Action Hook<\/strong> trong WordPress l\u00e0 m\u1ed9t k\u1ef9 n\u0103ng r\u1ea5t h\u1eefu \u00edch \u0111\u1ec3 t\u00f9y ch\u1ec9nh v\u00e0 m\u1edf r\u1ed9ng ch\u1ee9c n\u0103ng c\u1ee7a website m\u00e0 kh\u00f4ng c\u1ea7n ch\u1ec9nh s\u1eeda tr\u1ef1c ti\u1ebfp v\u00e0o core ho\u1eb7c theme\/plugin.<\/p>\n\n\n\n<p><strong>Action Hook l\u00e0 g\u00ec?<\/strong><\/p>\n\n\n\n<p>Trong WordPress, action hook l\u00e0 nh\u1eefng &#8220;\u0111i\u1ec3m neo&#8221; \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a s\u1eb5n trong m\u00e3 ngu\u1ed3n. T\u1ea1i nh\u1eefng \u0111i\u1ec3m n\u00e0y, b\u1ea1n c\u00f3 th\u1ec3 &#8220;m\u00f3c&#8221; (hook) c\u00e1c h\u00e0m t\u00f9y ch\u1ec9nh c\u1ee7a m\u00ecnh v\u00e0o \u0111\u1ec3 th\u1ef1c thi m\u1ed9t h\u00e0nh \u0111\u1ed9ng n\u00e0o \u0111\u00f3 khi WordPress ch\u1ea1y qua \u0111i\u1ec3m neo \u0111\u00f3.<\/p>\n\n\n\n<p><strong>1. C\u00e1c b\u01b0\u1edbc t\u1ea1o custom action hook:<\/strong><\/p>\n\n\n\n<p><strong>Ch\u1ecdn m\u1ed9t v\u1ecb tr\u00ed th\u00edch h\u1ee3p:<\/strong> X\u00e1c \u0111\u1ecbnh v\u1ecb tr\u00ed trong m\u00e3 ngu\u1ed3n theme ho\u1eb7c plugin c\u1ee7a b\u1ea1n n\u01a1i b\u1ea1n mu\u1ed1n &#8220;g\u1eafn&#8221; action hook. V\u1ecb tr\u00ed n\u00e0y th\u01b0\u1eddng l\u00e0 n\u01a1i b\u1ea1n mu\u1ed1n th\u1ef1c hi\u1ec7n m\u1ed9t h\u00e0nh \u0111\u1ed9ng t\u00f9y ch\u1ec9nh. V\u00ed d\u1ee5:<\/p>\n\n\n\n<p>Tr\u01b0\u1edbc khi hi\u1ec3n th\u1ecb n\u1ed9i dung m\u1ed9t b\u00e0i vi\u1ebft.<\/p>\n\n\n\n<p>Sau khi l\u01b0u m\u1ed9t b\u00e0i vi\u1ebft.<\/p>\n\n\n\n<p>Trong qu\u00e1 tr\u00ecnh x\u1eed l\u00fd form.<\/p>\n\n\n\n<p>B\u1ea5t k\u1ef3 v\u1ecb tr\u00ed n\u00e0o b\u1ea1n th\u1ea5y ph\u00f9 h\u1ee3p v\u1edbi nhu c\u1ea7u c\u1ee7a m\u00ecnh.<\/p>\n\n\n\n<p><strong>2. Khai b\u00e1o action hook b\u1eb1ng <code>do_action()<\/code>:<\/strong><\/p>\n\n\n\n<p>T\u1ea1i v\u1ecb tr\u00ed b\u1ea1n \u0111\u00e3 ch\u1ecdn, h\u00e3y th\u00eam \u0111o\u1ea1n m\u00e3 PHP sau \u0111\u1ec3 khai b\u00e1o action hook:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\ndo_action( &#039;ten_action_hook_cua_ban&#039; );\n<\/pre><\/div>\n\n\n<p>Thay th\u1ebf <code>'ten_action_hook_cua_ban'<\/code> b\u1eb1ng m\u1ed9t t\u00ean duy nh\u1ea5t v\u00e0 d\u1ec5 hi\u1ec3u, th\u01b0\u1eddng s\u1eed d\u1ee5ng ti\u1ec1n t\u1ed1 c\u1ee7a theme ho\u1eb7c plugin c\u1ee7a b\u1ea1n \u0111\u1ec3 tr\u00e1nh xung \u0111\u1ed9t t\u00ean v\u1edbi c\u00e1c action hook kh\u00e1c.<\/p>\n\n\n\n<p>V\u00ed d\u1ee5: <code>tccntt_after_content<\/code>, <code>tccntt_before_save<\/code>.<\/p>\n\n\n\n<p>B\u1ea1n c\u0169ng c\u00f3 th\u1ec3 truy\u1ec1n d\u1eef li\u1ec7u v\u00e0o action hook b\u1eb1ng c\u00e1ch th\u00eam c\u00e1c tham s\u1ed1 sau t\u00ean hook:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\ndo_action( &#039;ten_action_hook_cua_ban&#039;, $arg1, $arg2, ... );\n<\/pre><\/div>\n\n\n<p><strong>T\u1ea1o h\u00e0m x\u1eed l\u00fd action:<\/strong> B\u00e2y gi\u1edd, b\u1ea1n c\u1ea7n t\u1ea1o m\u1ed9t h\u00e0m PHP \u0111\u1ec3 th\u1ef1c hi\u1ec7n h\u00e0nh \u0111\u1ed9ng b\u1ea1n mu\u1ed1n khi action hook \u0111\u01b0\u1ee3c g\u1ecdi. V\u00ed d\u1ee5:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nfunction my_custom_action_function() {\n    \/\/ M\u00e3 PHP b\u1ea1n mu\u1ed1n th\u1ef1c thi t\u1ea1i hook n\u00e0y\n    echo &#039;&lt;p&gt;\u0110\u00e2y l\u00e0 h\u00e0nh \u0111\u1ed9ng t\u00f9y ch\u1ec9nh c\u1ee7a t\u00f4i!&lt;\/p&gt;&#039;;\n}\n<\/pre><\/div>\n\n\n<p>N\u1ebfu b\u1ea1n \u0111\u00e3 truy\u1ec1n d\u1eef li\u1ec7u v\u00e0o <code>do_action()<\/code>, h\u00e0m c\u1ee7a b\u1ea1n c\u0169ng c\u1ea7n khai b\u00e1o c\u00e1c tham s\u1ed1 t\u01b0\u01a1ng \u1ee9ng \u0111\u1ec3 nh\u1eadn d\u1eef li\u1ec7u \u0111\u00f3:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nfunction my_custom_action_function( $arg1, $arg2 ) {\n    \/\/ S\u1eed d\u1ee5ng $arg1 v\u00e0 $arg2 trong h\u00e0m c\u1ee7a b\u1ea1n\n    echo &#039;&lt;p&gt;Gi\u00e1 tr\u1ecb tham s\u1ed1 1: &#039; . esc_html( $arg1 ) . &#039;&lt;\/p&gt;&#039;;\n    echo &#039;&lt;p&gt;Gi\u00e1 tr\u1ecb tham s\u1ed1 2: &#039; . esc_html( $arg2 ) . &#039;&lt;\/p&gt;&#039;;\n}\n<\/pre><\/div>\n\n\n<p>&#8220;<strong>M\u00f3c&#8221; h\u00e0m v\u00e0o action hook b\u1eb1ng <code>add_action()<\/code><\/strong><\/p>\n\n\n\n<p>Cu\u1ed1i c\u00f9ng, b\u1ea1n c\u1ea7n s\u1eed d\u1ee5ng h\u00e0m <code>add_action()<\/code> \u0111\u1ec3 li\u00ean k\u1ebft h\u00e0m x\u1eed l\u00fd c\u1ee7a b\u1ea1n v\u1edbi action hook \u0111\u00e3 t\u1ea1o. Th\u00f4ng th\u01b0\u1eddng, b\u1ea1n s\u1ebd \u0111\u1eb7t \u0111o\u1ea1n m\u00e3 n\u00e0y trong file <code>functions.php<\/code> c\u1ee7a theme ho\u1eb7c trong file ch\u00ednh c\u1ee7a plugin.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nadd_action( &#039;ten_action_hook_cua_ban&#039;, &#039;my_custom_action_function&#039; );\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>Tham s\u1ed1 \u0111\u1ea7u ti\u00ean l\u00e0 t\u00ean c\u1ee7a action hook b\u1ea1n \u0111\u00e3 khai b\u00e1o.<\/li>\n\n\n\n<li>Tham s\u1ed1 th\u1ee9 hai l\u00e0 t\u00ean c\u1ee7a h\u00e0m x\u1eed l\u00fd b\u1ea1n \u0111\u00e3 t\u1ea1o.<\/li>\n\n\n\n<li>B\u1ea1n c\u0169ng c\u00f3 th\u1ec3 ch\u1ec9 \u0111\u1ecbnh \u0111\u1ed9 \u01b0u ti\u00ean (tham s\u1ed1 th\u1ee9 ba, s\u1ed1 c\u00e0ng nh\u1ecf th\u00ec \u0111\u1ed9 \u01b0u ti\u00ean c\u00e0ng cao, m\u1eb7c \u0111\u1ecbnh l\u00e0 10) v\u00e0 s\u1ed1 l\u01b0\u1ee3ng tham s\u1ed1 m\u00e0 h\u00e0m c\u1ee7a b\u1ea1n ch\u1ea5p nh\u1eadn (tham s\u1ed1 th\u1ee9 t\u01b0, m\u1eb7c \u0111\u1ecbnh l\u00e0 1)<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nadd_action( &#039;ten_action_hook_cua_ban&#039;, &#039;my_custom_action_function&#039;, 20, 2 );\n<\/pre><\/div>\n\n\n<p>Gi\u1ea3 s\u1eed b\u1ea1n mu\u1ed1n th\u00eam m\u1ed9t \u0111o\u1ea1n text t\u00f9y ch\u1ec9nh sau ti\u00eau \u0111\u1ec1 c\u1ee7a m\u1ed7i b\u00e0i vi\u1ebft.<\/p>\n\n\n\n<p><strong>Ch\u1ecdn v\u1ecb tr\u00ed v\u00e0 khai b\u00e1o action hook:<\/strong> Trong file <code>single.php<\/code> c\u1ee7a theme b\u1ea1n (ho\u1eb7c m\u1ed9t template part hi\u1ec3n th\u1ecb ti\u00eau \u0111\u1ec1), b\u1ea1n c\u00f3 th\u1ec3 th\u00eam:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;h1&gt;&lt;?php the_title(); ?&gt;&lt;\/h1&gt;\n&lt;?php do_action( &#039;tccntt_after_post_title&#039; ); ?&gt;\n<\/pre><\/div>\n\n\n<p><strong>T\u1ea1o h\u00e0m x\u1eed l\u00fd:<\/strong> Trong file <code>functions.php<\/code> c\u1ee7a theme<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nfunction tccntt_add_custom_text_after_title() {\n    echo &#039;&lt;div class=&quot;custom-text&quot;&gt;\u0110\u00e2y l\u00e0 \u0111o\u1ea1n text t\u00f9y ch\u1ec9nh sau ti\u00eau \u0111\u1ec1.&lt;\/div&gt;&#039;;\n}\n<\/pre><\/div>\n\n\n<p><strong>&#8220;M\u00f3c&#8221; h\u00e0m v\u00e0o action hook:<\/strong> C\u0169ng trong file <code>functions.php<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nadd_action( &#039;tccntt_after_post_title&#039;, &#039;tccntt_add_custom_text_after_title&#039; );\n<\/pre><\/div>\n\n\n<p>B\u00e2y gi\u1edd, khi b\u1ea1n xem m\u1ed9t b\u00e0i vi\u1ebft \u0111\u01a1n l\u1ebb, \u0111o\u1ea1n text &#8220;\u0110\u00e2y l\u00e0 \u0111o\u1ea1n text t\u00f9y ch\u1ec9nh sau ti\u00eau \u0111\u1ec1.&#8221; s\u1ebd hi\u1ec3n th\u1ecb ngay b\u00ean d\u01b0\u1edbi ti\u00eau \u0111\u1ec1.<\/p>\n\n\n\n<p><strong>L\u01b0u \u00fd quan tr\u1ecdng:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>T\u00ednh duy nh\u1ea5t c\u1ee7a t\u00ean hook:<\/strong> H\u00e3y \u0111\u1ea3m b\u1ea3o t\u00ean action hook c\u1ee7a b\u1ea1n l\u00e0 duy nh\u1ea5t \u0111\u1ec3 tr\u00e1nh xung \u0111\u1ed9t kh\u00f4ng mong mu\u1ed1n v\u1edbi c\u00e1c theme ho\u1eb7c plugin kh\u00e1c. S\u1eed d\u1ee5ng ti\u1ec1n t\u1ed1 ri\u00eang l\u00e0 m\u1ed9t c\u00e1ch t\u1ed1t \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o \u0111i\u1ec1u n\u00e0y.<\/li>\n\n\n\n<li><strong>V\u1ecb tr\u00ed khai b\u00e1o <code>do_action()<\/code>:<\/strong> Vi\u1ec7c ch\u1ecdn \u0111\u00fang v\u1ecb tr\u00ed \u0111\u1ec3 khai b\u00e1o action hook l\u00e0 r\u1ea5t quan tr\u1ecdng \u0111\u1ec3 h\u00e0nh \u0111\u1ed9ng c\u1ee7a b\u1ea1n \u0111\u01b0\u1ee3c th\u1ef1c thi \u0111\u00fang th\u1eddi \u0111i\u1ec3m.<\/li>\n\n\n\n<li><strong>N\u01a1i \u0111\u1eb7t <code>add_action()<\/code>:<\/strong> Th\u00f4ng th\u01b0\u1eddng, c\u00e1c h\u00e0m <code>add_action()<\/code> \u0111\u01b0\u1ee3c \u0111\u1eb7t trong file <code>functions.php<\/code> c\u1ee7a theme ho\u1eb7c trong file ch\u00ednh c\u1ee7a plugin. \u0110i\u1ec1u n\u00e0y \u0111\u1ea3m b\u1ea3o ch\u00fang \u0111\u01b0\u1ee3c t\u1ea3i khi WordPress kh\u1edfi \u0111\u1ed9ng.<\/li>\n<\/ul>\n\n\n\n<p>Hy v\u1ecdng h\u01b0\u1edbng d\u1eabn n\u00e0y gi\u00fap b\u1ea1n hi\u1ec3u r\u00f5 c\u00e1ch t\u1ea1o custom action hook trong WordPress.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>T\u1ea1o custom Action Hook trong WordPress l\u00e0 m\u1ed9t k\u1ef9 n\u0103ng r\u1ea5t h\u1eefu \u00edch \u0111\u1ec3 t\u00f9y ch\u1ec9nh v\u00e0 m\u1edf r\u1ed9ng ch\u1ee9c [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":9614,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[241],"tags":[],"class_list":["post-9848","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hooks"],"views":1323,"_links":{"self":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/9848","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/comments?post=9848"}],"version-history":[{"count":4,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/9848\/revisions"}],"predecessor-version":[{"id":9852,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/9848\/revisions\/9852"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media\/9614"}],"wp:attachment":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media?parent=9848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/categories?post=9848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/tags?post=9848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}