
{"id":9610,"date":"2025-04-14T13:44:45","date_gmt":"2025-04-14T06:44:45","guid":{"rendered":"https:\/\/tapchicntt.com\/?p=9610"},"modified":"2025-04-15T10:41:59","modified_gmt":"2025-04-15T03:41:59","slug":"wordpress-huong-dan-su-dung-wp_admin_notice","status":"publish","type":"post","link":"https:\/\/tapchicntt.com\/wordpress-huong-dan-su-dung-wp_admin_notice\/","title":{"rendered":"[WordPress] H\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng wp_admin_notice()"},"content":{"rendered":"\n<p>H\u00e0m <code>wp_admin_notice()<\/code> \u0111\u01b0\u1ee3c gi\u1edbi thi\u1ec7u trong WordPress 6.4.0, cung c\u1ea5p m\u1ed9t c\u00e1ch ti\u00eau chu\u1ea9n v\u00e0 d\u1ec5 d\u00e0ng h\u01a1n \u0111\u1ec3 hi\u1ec3n th\u1ecb c\u00e1c th\u00f4ng b\u00e1o qu\u1ea3n tr\u1ecb (admin notices) cho ng\u01b0\u1eddi d\u00f9ng trong khu v\u1ef1c qu\u1ea3n tr\u1ecb WordPress.<\/p>\n\n\n\n<p>Thay v\u00ec ph\u1ea3i t\u1ef1 t\u1ea1o m\u00e3 HTML ph\u1ee9c t\u1ea1p, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng h\u00e0m n\u00e0y \u0111\u1ec3 t\u1ea1o c\u00e1c th\u00f4ng b\u00e1o c\u00f3 ki\u1ec3u d\u00e1ng nh\u1ea5t qu\u00e1n v\u00e0 kh\u1ea3 n\u0103ng lo\u1ea1i b\u1ecf (dismissible).<\/p>\n\n\n\n<p><strong>C\u00fa ph\u00e1p:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nwp_admin_notice( string $message, array $args = array() )\n<\/pre><\/div>\n\n\n<p>T<strong>ham s\u1ed1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$message<\/code> (string) (B\u1eaft bu\u1ed9c): N\u1ed9i dung c\u1ee7a th\u00f4ng b\u00e1o b\u1ea1n mu\u1ed1n hi\u1ec3n th\u1ecb.<\/li>\n\n\n\n<li><code>$args<\/code> (array) (T\u00f9y ch\u1ecdn): M\u1ed9t m\u1ea3ng c\u00e1c \u0111\u1ed1i s\u1ed1 \u0111\u1ec3 t\u00f9y ch\u1ec9nh th\u00f4ng b\u00e1o. C\u00e1c kh\u00f3a (keys) ph\u1ed5 bi\u1ebfn bao g\u1ed3m:\n<ul class=\"wp-block-list\">\n<li><code>type<\/code> (string): Lo\u1ea1i th\u00f4ng b\u00e1o. C\u00e1c gi\u00e1 tr\u1ecb \u0111\u01b0\u1ee3c h\u1ed7 tr\u1ee3 l\u00e0 <code>'success'<\/code>, <code>'error'<\/code>, <code>'warning'<\/code>, <code>'info'<\/code>. N\u1ebfu kh\u00f4ng \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh, n\u00f3 s\u1ebd l\u00e0 m\u1ed9t th\u00f4ng b\u00e1o m\u1eb7c \u0111\u1ecbnh.<\/li>\n\n\n\n<li><code>dismissible<\/code> (bool): X\u00e1c \u0111\u1ecbnh xem th\u00f4ng b\u00e1o c\u00f3 th\u1ec3 b\u1ecb ng\u01b0\u1eddi d\u00f9ng lo\u1ea1i b\u1ecf hay kh\u00f4ng. Gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh l\u00e0 <code>false<\/code>.<\/li>\n\n\n\n<li><code>id<\/code> (string): Gi\u00e1 tr\u1ecb cho thu\u1ed9c t\u00ednh <code>id<\/code> c\u1ee7a ph\u1ea7n t\u1eed HTML bao b\u1ecdc th\u00f4ng b\u00e1o. Gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh l\u00e0 m\u1ed9t chu\u1ed7i r\u1ed7ng.<\/li>\n\n\n\n<li><code>additional_classes<\/code> (string|array): M\u1ed9t chu\u1ed7i ho\u1eb7c m\u1ed9t m\u1ea3ng c\u00e1c t\u00ean l\u1edbp CSS b\u1ed5 sung \u0111\u1ec3 th\u00eam v\u00e0o ph\u1ea7n t\u1eed HTML bao b\u1ecdc th\u00f4ng b\u00e1o. Gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh l\u00e0 m\u1ed9t m\u1ea3ng r\u1ed7ng.<\/li>\n\n\n\n<li><code>attributes<\/code> (array): M\u1ed9t m\u1ea3ng c\u00e1c thu\u1ed9c t\u00ednh HTML b\u1ed5 sung (v\u00ed d\u1ee5: <code>['data-custom' =&gt; 'value']<\/code>). Gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh l\u00e0 m\u1ed9t m\u1ea3ng r\u1ed7ng.<\/li>\n\n\n\n<li><code>paragraph_wrap<\/code> (bool): X\u00e1c \u0111\u1ecbnh xem th\u00f4ng b\u00e1o c\u00f3 n\u00ean \u0111\u01b0\u1ee3c bao b\u1ecdc trong th\u1ebb <code>&lt;p&gt;<\/code> hay kh\u00f4ng. Gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh l\u00e0 <code>true<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>C\u00e1ch s\u1eed d\u1ee5ng:<\/strong><\/p>\n\n\n\n<p>\u0110\u1ec3 hi\u1ec3n th\u1ecb m\u1ed9t th\u00f4ng b\u00e1o qu\u1ea3n tr\u1ecb, b\u1ea1n c\u1ea7n &#8220;hook&#8221; h\u00e0m c\u1ee7a m\u00ecnh v\u00e0o m\u1ed9t action hook th\u00edch h\u1ee3p trong khu v\u1ef1c qu\u1ea3n tr\u1ecb. Action hook ph\u1ed5 bi\u1ebfn nh\u1ea5t cho vi\u1ec7c n\u00e0y l\u00e0 <code>admin_notices<\/code> (hi\u1ec3n th\u1ecb tr\u00ean t\u1ea5t c\u1ea3 c\u00e1c trang qu\u1ea3n tr\u1ecb) ho\u1eb7c c\u00e1c action hook c\u1ee5 th\u1ec3 h\u01a1n cho t\u1eebng trang (v\u00ed d\u1ee5: <code>admin_init<\/code> cho m\u1ed9t s\u1ed1 t\u00e1c v\u1ee5 nh\u1ea5t \u0111\u1ecbnh).<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5 c\u01a1 b\u1ea3n:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\nfunction my_admin_notice() {\n    wp_admin_notice( &#039;\u0110\u00e2y l\u00e0 m\u1ed9t th\u00f4ng b\u00e1o qu\u1ea3n tr\u1ecb c\u01a1 b\u1ea3n.&#039; );\n}\nadd_action( &#039;admin_notices&#039;, &#039;my_admin_notice&#039; );\n?&gt;\n<\/pre><\/div>\n\n\n<p>\u0110o\u1ea1n m\u00e3 n\u00e0y s\u1ebd hi\u1ec3n th\u1ecb m\u1ed9t th\u00f4ng b\u00e1o m\u1eb7c \u0111\u1ecbnh tr\u00ean t\u1ea5t c\u1ea3 c\u00e1c trang qu\u1ea3n tr\u1ecb.<\/p>\n\n\n\n<p><strong>V\u00ed d\u1ee5 v\u1edbi c\u00e1c lo\u1ea1i th\u00f4ng b\u00e1o kh\u00e1c nhau:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\nfunction my_success_notice() {\n    wp_admin_notice( &#039;Thao t\u00e1c \u0111\u00e3 th\u00e0nh c\u00f4ng!&#039;, &#x5B;\n        &#039;type&#039; =&gt; &#039;success&#039;,\n        &#039;dismissible&#039; =&gt; true,\n    ] );\n}\nadd_action( &#039;admin_notices&#039;, &#039;my_success_notice&#039; );\n\nfunction my_error_notice() {\n    wp_admin_notice( &#039;\u0110\u00e3 x\u1ea3y ra l\u1ed7i. Vui l\u00f2ng ki\u1ec3m tra l\u1ea1i.&#039;, &#x5B;\n        &#039;type&#039; =&gt; &#039;error&#039;,\n    ] );\n}\nadd_action( &#039;admin_notices&#039;, &#039;my_error_notice&#039; );\n\nfunction my_warning_notice() {\n    wp_admin_notice( &#039;C\u1ea3nh b\u00e1o: H\u00e3y c\u1ea9n th\u1eadn khi th\u1ef1c hi\u1ec7n h\u00e0nh \u0111\u1ed9ng n\u00e0y.&#039;, &#x5B;\n        &#039;type&#039; =&gt; &#039;warning&#039;,\n        &#039;additional_classes&#039; =&gt; &#039;my-custom-warning&#039;,\n    ] );\n}\nadd_action( &#039;admin_notices&#039;, &#039;my_warning_notice&#039; );\n\nfunction my_info_notice() {\n    wp_admin_notice( &#039;Th\u00f4ng tin: \u0110\u00e2y l\u00e0 m\u1ed9t th\u00f4ng b\u00e1o th\u00f4ng tin.&#039;, &#x5B;\n        &#039;type&#039; =&gt; &#039;info&#039;,\n    ] );\n}\nadd_action( &#039;admin_notices&#039;, &#039;my_info_notice&#039; );\n?&gt;\n<\/pre><\/div>\n\n\n<p><strong>N\u01a1i \u0111\u1eb7t m\u00e3:<\/strong><\/p>\n\n\n\n<p>B\u1ea1n c\u00f3 th\u1ec3 \u0111\u1eb7t \u0111o\u1ea1n m\u00e3 n\u00e0y trong:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>T\u1ec7p <code>functions.php<\/code> c\u1ee7a theme hi\u1ec7n t\u1ea1i:<\/strong> C\u00e1ch n\u00e0y ph\u00f9 h\u1ee3p cho c\u00e1c t\u00f9y ch\u1ec9nh li\u00ean quan \u0111\u1ebfn theme. Tuy nhi\u00ean, th\u00f4ng b\u00e1o s\u1ebd bi\u1ebfn m\u1ea5t n\u1ebfu b\u1ea1n chuy\u1ec3n theme.<\/li>\n\n\n\n<li><strong>Plugin t\u00f9y ch\u1ec9nh:<\/strong> \u0110\u00e2y l\u00e0 c\u00e1ch t\u1ed1t nh\u1ea5t \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o th\u00f4ng b\u00e1o v\u1eabn hi\u1ec3n th\u1ecb ngay c\u1ea3 khi b\u1ea1n thay \u0111\u1ed5i theme.<\/li>\n<\/ul>\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 nh\u1ea5t qu\u00e1n:<\/strong> S\u1eed d\u1ee5ng <code>wp_admin_notice()<\/code> gi\u00fap \u0111\u1ea3m b\u1ea3o giao di\u1ec7n th\u00f4ng b\u00e1o nh\u1ea5t qu\u00e1n v\u1edbi c\u00e1c th\u00f4ng b\u00e1o kh\u00e1c c\u1ee7a WordPress.<\/li>\n\n\n\n<li><strong>Kh\u1ea3 n\u0103ng lo\u1ea1i b\u1ecf:<\/strong> Cung c\u1ea5p t\u00f9y ch\u1ecdn <code>dismissible<\/code> cho c\u00e1c th\u00f4ng b\u00e1o kh\u00f4ng quan tr\u1ecdng \u0111\u1ec3 tr\u00e1nh l\u00e0m ng\u01b0\u1eddi d\u00f9ng kh\u00f3 ch\u1ecbu.<\/li>\n\n\n\n<li><strong>R\u00f5 r\u00e0ng v\u00e0 ng\u1eafn g\u1ecdn:<\/strong> N\u1ed9i dung th\u00f4ng b\u00e1o n\u00ean r\u00f5 r\u00e0ng, d\u1ec5 hi\u1ec3u v\u00e0 \u0111i th\u1eb3ng v\u00e0o v\u1ea5n \u0111\u1ec1. Tr\u00e1nh s\u1eed d\u1ee5ng ng\u00f4n ng\u1eef m\u01a1 h\u1ed3 ho\u1eb7c qu\u00e1 nhi\u1ec1u chi ti\u1ebft kh\u00f4ng c\u1ea7n thi\u1ebft.<\/li>\n\n\n\n<li><strong>Li\u00ean quan v\u00e0 k\u1ecbp th\u1eddi:<\/strong> Ch\u1ec9 hi\u1ec3n th\u1ecb c\u00e1c th\u00f4ng b\u00e1o th\u1ef1c s\u1ef1 c\u1ea7n thi\u1ebft v\u00e0 li\u00ean quan \u0111\u1ebfn ng\u1eef c\u1ea3nh hi\u1ec7n t\u1ea1i c\u1ee7a ng\u01b0\u1eddi d\u00f9ng. C\u00e1c th\u00f4ng b\u00e1o l\u1ed7i th\u1eddi ho\u1eb7c kh\u00f4ng li\u00ean quan c\u00f3 th\u1ec3 l\u00e0m r\u1ed1i giao di\u1ec7n qu\u1ea3n tr\u1ecb.<\/li>\n\n\n\n<li><strong>Tr\u00e1nh l\u1ea1m d\u1ee5ng:<\/strong> S\u1eed d\u1ee5ng th\u00f4ng b\u00e1o qu\u1ea3n tr\u1ecb m\u1ed9t c\u00e1ch h\u1ee3p l\u00fd. Qu\u00e1 nhi\u1ec1u th\u00f4ng b\u00e1o c\u00f3 th\u1ec3 g\u00e2y xao nh\u00e3ng v\u00e0 l\u00e0m gi\u1ea3m tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng.<\/li>\n\n\n\n<li><strong>Kh\u1ea3 n\u0103ng truy c\u1eadp:<\/strong> \u0110\u1ea3m b\u1ea3o th\u00f4ng b\u00e1o c\u1ee7a b\u1ea1n c\u00f3 th\u1ec3 truy c\u1eadp \u0111\u01b0\u1ee3c \u0111\u1ed1i v\u1edbi t\u1ea5t c\u1ea3 ng\u01b0\u1eddi d\u00f9ng, bao g\u1ed3m c\u1ea3 nh\u1eefng ng\u01b0\u1eddi c\u00f3 khuy\u1ebft t\u1eadt. S\u1eed d\u1ee5ng m\u00e0u s\u1eafc t\u01b0\u01a1ng ph\u1ea3n cao cho v\u0103n b\u1ea3n v\u00e0 n\u1ec1n, \u0111\u1ed3ng th\u1eddi cung c\u1ea5p v\u0103n b\u1ea3n thay th\u1ebf cho h\u00ecnh \u1ea3nh (n\u1ebfu c\u00f3).<\/li>\n\n\n\n<li><strong>Ki\u1ec3m tra:<\/strong> Ki\u1ec3m tra th\u00f4ng b\u00e1o tr\u00ean c\u00e1c tr\u00ecnh duy\u1ec7t v\u00e0 thi\u1ebft b\u1ecb kh\u00e1c nhau \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o ch\u00fang hi\u1ec3n th\u1ecb \u0111\u00fang c\u00e1ch.<\/li>\n\n\n\n<li><strong>B\u1ea3o m\u1eadt:<\/strong> Lu\u00f4n tu\u00e2n th\u1ee7 c\u00e1c nguy\u00ean t\u1eafc b\u1ea3o m\u1eadt khi t\u1ea1o th\u00f4ng b\u00e1o, \u0111\u1eb7c bi\u1ec7t l\u00e0 khi hi\u1ec3n th\u1ecb d\u1eef li\u1ec7u ng\u01b0\u1eddi d\u00f9ng ho\u1eb7c th\u00f4ng tin nh\u1ea1y c\u1ea3m. \u0110\u1ea3m b\u1ea3o m\u1ecdi d\u1eef li\u1ec7u \u0111\u1ec1u \u0111\u01b0\u1ee3c l\u00e0m s\u1ea1ch v\u00e0 x\u00e1c th\u1ef1c \u0111\u1ec3 tr\u00e1nh c\u00e1c l\u1ed7 h\u1ed5ng b\u1ea3o m\u1eadt nh\u01b0 t\u1ea5n c\u00f4ng XSS.<\/li>\n<\/ul>\n\n\n\n<p>S\u1eed d\u1ee5ng <code>wp_admin_notice()<\/code> l\u00e0 m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3 v\u00e0 d\u1ec5 d\u00e0ng \u0111\u1ec3 giao ti\u1ebfp v\u1edbi ng\u01b0\u1eddi d\u00f9ng trong khu v\u1ef1c qu\u1ea3n tr\u1ecb WordPress, gi\u00fap c\u1ea3i thi\u1ec7n tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng v\u00e0 cung c\u1ea5p th\u00f4ng tin quan tr\u1ecdng m\u1ed9t c\u00e1ch tr\u1ef1c quan.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>H\u00e0m wp_admin_notice() \u0111\u01b0\u1ee3c gi\u1edbi thi\u1ec7u trong WordPress 6.4.0, cung c\u1ea5p m\u1ed9t c\u00e1ch ti\u00eau chu\u1ea9n v\u00e0 d\u1ec5 d\u00e0ng h\u01a1n \u0111\u1ec3 hi\u1ec3n [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":9641,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[244],"tags":[],"class_list":["post-9610","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-functions"],"views":749,"_links":{"self":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/9610","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=9610"}],"version-history":[{"count":2,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/9610\/revisions"}],"predecessor-version":[{"id":9644,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/9610\/revisions\/9644"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media\/9641"}],"wp:attachment":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media?parent=9610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/categories?post=9610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/tags?post=9610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}