
{"id":3188,"date":"2014-02-20T10:59:14","date_gmt":"2014-02-20T10:59:14","guid":{"rendered":"http:\/\/tapchicntt.com\/?p=3188"},"modified":"2023-08-09T23:49:16","modified_gmt":"2023-08-09T16:49:16","slug":"huong-dan-tao-meta-box-cho-custom-field-trong-wordpress","status":"publish","type":"post","link":"https:\/\/tapchicntt.com\/huong-dan-tao-meta-box-cho-custom-field-trong-wordpress\/","title":{"rendered":"[WordPress] H\u01b0\u1edbng d\u1eabn t\u1ea1o Meta Box cho Custom Field trong WordPress"},"content":{"rendered":"\n<p>Ch\u00e0o c\u00e1c b\u1ea1n, trong b\u00e0i h\u01b0\u1edbng d\u1eabn tr\u01b0\u1edbc ch\u00fang t\u00f4i \u0111\u00e3 h\u01b0\u1edbng d\u1eabn c\u00e1c b\u1ea1n c\u00e1ch s\u1eed d\u1ee5ng v\u00e0 l\u1ea5y d\u1eef li\u1ec7u c\u1ee7a Custom Field trong WordPress (WP).<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><img decoding=\"async\" src=\"http:\/\/upload.lyminhhoang.com\/repository\/2017\/su-dung-custom-field-cua-wordpress\/su-dung-custom-field-cua-wordpress-01.jpg\" alt=\"\"><br>Nh\u01b0ng v\u1edbi c\u00e1c th\u00eam Custom Field \u0111\u00f3 b\u1ea1n ph\u1ea3i ch\u1ecdn &#8220;Name&#8221; v\u00e0 th\u00eam gi\u00e1 tr\u1ecb. B\u00e0i vi\u1ebft n\u00e0y ch\u00fang t\u00f4i s\u1ebd h\u01b0\u1edbng d\u1eabn c\u00e1c b\u1ea1n t\u1ea1o Meta Box ch\u00e8n d\u1eef li\u1ec7u cho Custom Field.<\/p>\n\n\n\n<p>Tr\u01b0\u1edbc ti\u00ean b\u1ea1n m\u1edf file file <code>functions.php<\/code> v\u00e0 th\u00eam \u0111o\u1ea1n code sau:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; quick-code: false; notranslate\" title=\"\">\n\/* Meta Post ----------------*\/\n\n$post_in_tapchicntt =\n  array(\n  &quot;price&quot; =&gt;\n\t  array(\n\t\t  &quot;name&quot; =&gt; &quot;price&quot;,\n\t\t  &quot;type&quot; =&gt; &quot;text&quot;,\n\t\t  &quot;std&quot; =&gt; &quot;&quot;,\n\t\t  &quot;title&quot; =&gt; &quot;Price&quot;,\n\t\t  &quot;description&quot; =&gt; &quot;Gi\u00e1 B\u00e1n&quot;\n\t),\n\t&quot;discount&quot; =&gt;\n\t  array(\n\t\t  &quot;name&quot; =&gt; &quot;discount&quot;,\n\t\t  &quot;type&quot; =&gt; &quot;text&quot;,\n\t\t  &quot;std&quot; =&gt; &quot;&quot;,\n\t\t  &quot;title&quot; =&gt; &quot;Discount&quot;,\n\t\t  &quot;description&quot; =&gt; &quot;Gi\u00e1 Gi\u1ea3m&quot;\n\t),\n\t&quot;sku&quot; =&gt;\n\t  array(\n\t\t  &quot;name&quot; =&gt; &quot;sku&quot;,\n\t\t  &quot;type&quot; =&gt; &quot;text&quot;,\n\t\t  &quot;std&quot; =&gt; &quot;&quot;,\n\t\t  &quot;title&quot; =&gt; &quot;Code Product (SKU)&quot;,\n\t\t  &quot;description&quot; =&gt; &quot;&quot;\n\t)\n );\n\nfunction post_in_tapchicntt() {\n  global $post, $post_in_tapchicntt;\n  $output = &#039;&lt;style&gt;\n  \t\t\t\tlabel#tutorial_custom { display: block; font-weight: bold; padding: 5px;}\n\t\t\t\tinput.tutorial_custom { padding: 5px}\n  \t\t\t&lt;\/style&gt;&#039;;\n  foreach($post_in_tapchicntt as $meta_box) {\n    $meta_box_value = get_post_meta($post-&gt;ID, $meta_box&#x5B;&#039;name&#039;], true);\n\n    if($meta_box_value == &quot;&quot;)\n      $meta_box_value = $meta_box&#x5B;&#039;std&#039;];\n\n\tif ($meta_box&#x5B;&#039;type&#039;] == &#039;text&#039; ){\n\t\t$output .= &#039;&lt;input type=&quot;hidden&quot; name=&quot;&#039;.$meta_box&#x5B;&#039;name&#039;].&#039;_noncename&quot; id=&quot;&#039;.$meta_box&#x5B;&#039;name&#039;].&#039;_noncename&quot; value=&quot;&#039;.wp_create_nonce( plugin_basename(__FILE__) ).&#039;&quot; \/&gt;&#039;;\n\t\t$output .= &#039;&lt;label id=&quot;tutorial_custom&quot; for=&quot;&#039;.$meta_box&#x5B;&#039;name&#039;].&#039;&quot;&gt;&#039;.$meta_box&#x5B;&#039;title&#039;].&#039;&lt;\/label&gt;&#039;;\n\t\t$output .= &#039;&lt;input class=&quot;tutorial_custom&quot; type=&quot;&#039;.$meta_box&#x5B;&#039;type&#039;].&#039;&quot; name=&quot;tutorialblog_&#039;.$meta_box&#x5B;&#039;name&#039;].&#039;&quot; value=&quot;&#039;.$meta_box_value.&#039;&quot; width=&quot;90%&quot; \/&gt;&lt;br \/&gt;&#039;;\n\t\t$output .= $meta_box&#x5B;&#039;description&#039;].&#039;&lt;br \/&gt;&#039;;\n\t}\n  }\n  echo $output;\n\n}\n\nfunction create_meta_box() {\n  if ( function_exists(&#039;add_meta_box&#039;) ) {\n    add_meta_box( &#039;new-meta-boxes&#039;, &#039;Th\u00f4ng tin s\u1ea3n ph\u1ea9m&#039;, &#039;post_in_tapchicntt&#039;, &#039;post&#039;, &#039;normal&#039;, &#039;high&#039; );\n  }\n}\n\nfunction save_postdata( $post_id ) {\n  global $post, $post_in_tapchicntt;\n\n  foreach($post_in_tapchicntt as $meta_box) {\n  \/\/ Verify\n  if ( !wp_verify_nonce( $_POST&#x5B;$meta_box&#x5B;&#039;name&#039;].&#039;_noncename&#039;], plugin_basename(__FILE__) )) {\n    return $post_id;\n  }\n\n  if ( &#039;page&#039; == $_POST&#x5B;&#039;post_type&#039;] ) {\n  if ( !current_user_can( &#039;edit_page&#039;, $post_id ))\n    return $post_id;\n  } else {\n  if ( !current_user_can( &#039;edit_post&#039;, $post_id ))\n    return $post_id;\n  }\n\n\t$data = $_POST&#x5B;&#039;tutorialblog_&#039;.$meta_box&#x5B;&#039;name&#039;]];\n\n\tif(get_post_meta($post_id, $meta_box&#x5B;&#039;name&#039;]) == &quot;&quot;)\n\t\tadd_post_meta($post_id, $meta_box&#x5B;&#039;name&#039;], $data, true);\n\telseif($data != get_post_meta($post_id, $meta_box&#x5B;&#039;name&#039;], true))\n\t\tupdate_post_meta($post_id, $meta_box&#x5B;&#039;name&#039;], $data);\n\telseif($data == &quot;&quot;)\n\t\tdelete_post_meta($post_id, $meta_box&#x5B;&#039;name&#039;], get_post_meta($post_id, $meta_box&#x5B;&#039;name&#039;], true));\n\t  }\n\n}\n\nadd_action(&#039;admin_menu&#039;, &#039;create_meta_box&#039;);\nadd_action(&#039;save_post&#039;, &#039;save_postdata&#039;);\n<\/pre><\/div>\n\n\n<p>Ch\u00fac c\u00e1c b\u1ea1n th\u00e0nh c\u00f4ng!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ch\u00e0o c\u00e1c b\u1ea1n, trong b\u00e0i h\u01b0\u1edbng d\u1eabn tr\u01b0\u1edbc ch\u00fang t\u00f4i \u0111\u00e3 h\u01b0\u1edbng d\u1eabn c\u00e1c b\u1ea1n c\u00e1ch s\u1eed d\u1ee5ng v\u00e0 l\u1ea5y [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":6979,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[],"class_list":["post-3188","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"views":435,"_links":{"self":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/3188","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=3188"}],"version-history":[{"count":2,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/3188\/revisions"}],"predecessor-version":[{"id":8051,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/3188\/revisions\/8051"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media\/6979"}],"wp:attachment":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media?parent=3188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/categories?post=3188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/tags?post=3188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}