{"id":6245,"date":"2016-11-18T18:22:40","date_gmt":"2016-11-18T23:22:40","guid":{"rendered":"https:\/\/themespress.ca\/?p=6245"},"modified":"2022-09-05T11:38:09","modified_gmt":"2022-09-05T15:38:09","slug":"build-and-display-a-wordpress-metabox","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/build-and-display-a-wordpress-metabox\/","title":{"rendered":"Build and Display a WordPress Metabox"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>This tutorial will help you learn how to create a metabox allowing users to collect data from a Post or custom Post Type. In the example below, we will use  <strong>Themespress.ca<\/strong> as an example.<\/p>\n<p>We want to create a Metabox with the following structure:<br \/>\n\u2013 Easy<br \/>\n\u2013 Intermediate<br \/>\n\u2013 Hard<\/p>\n<p>This metabox will let you measure the post or post type for users. There are multiple ways to create a metabox. We recommend: <\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Build and Display a WordPress Metabox<\/h2><div id=\"ts-enlighterjs-container-1907922\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-1907922\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">function statut_diffucult_get_meta( $value ) {\n\tglobal $post;\n\t$field = get_post_meta( $post-&gt;ID, $value, true );\n\tif ( ! empty( $field ) ) {\n\t\treturn is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );\n\t} else {\n\t\treturn false;\n\t}\n}\n\nfunction statut_diffucult_add_meta_box() {\n\tadd_meta_box(\n\t\t&#039;statut_diffucult-statut-diffucult&#039;,\n\t\t__( &#039;Statut Difficult&eacute;&#039;, &#039;statut_diffucult&#039; ),\n\t\t&#039;statut_diffucult_html&#039;,\n                &#039;post&#039;,\n\t\t&#039;side&#039;,\n\t\t&#039;high&#039;\n\t);\n}\nadd_action( &#039;add_meta_boxes&#039;, &#039;statut_diffucult_add_meta_box&#039; );<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>In this function <strong>statut_hard_add_meta_box<\/strong>, we can figure out how to build our structure. We also use it for WordPress posts. (&#8216;post&#8217;) == \/\/ Changer pour votre Post Type.<\/p>\n<p>The side of our function represents the positioning in the Post Type.<\/p>\n<p>In this case, the side is the lateral bar of WordPress. The High defines its priority; in this case we want it higher because we want it at the top. The metabox is for the posts and will be positioned in the top right section of the post you&#8217;re currently editing.<\/p>\n<p>Now we want our metabox to include three checkbox; one for each choice: Easy, Intermediate, Hard. It&#8217;s also done so we can save the checkbox (or checkboxes) selected.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-2516055\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-2516055\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"standard\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">function statut_diffucult_html( $post) {\n\twp_nonce_field( &#039;_statut_diffucult_nonce&#039;, &#039;statut_diffucult_nonce&#039; ); ?&gt;\n\t&lt;p&gt;\n        &lt;input type=&quot;checkbox&quot; name=&quot;statut_diffucult_facile&quot; id=&quot;statut_diffucult_facile&quot; value=&quot;facile&quot; &lt;?php echo ( statut_diffucult_get_meta( &#039;statut_diffucult_facile&#039; ) === &#039;facile&#039; ) ? &#039;checked&#039; : &#039;&#039;; ?&gt;&gt;\n\t\t&lt;label for=&quot;statut_diffucult_facile&quot;&gt;&lt;?php _e( &#039;Facile&#039;, &#039;crio-child&#039; ); ?&gt;&lt;\/label&gt;\t\n\t\t\n\t&lt;\/p&gt;\t\n\t&lt;p&gt;\n        &lt;input type=&quot;checkbox&quot; name=&quot;statut_diffucult_intermdiaire&quot; id=&quot;statut_diffucult_intermdiaire&quot; value=&quot;intermdiaire&quot; &lt;?php echo ( statut_diffucult_get_meta( &#039;statut_diffucult_intermdiaire&#039; ) === &#039;intermdiaire&#039; ) ? &#039;checked&#039; : &#039;&#039;; ?&gt;&gt;\n\t\t&lt;label for=&quot;statut_diffucult_intermdiaire&quot;&gt;&lt;?php _e( &#039;Interm&eacute;diaire&#039;, &#039;crio-child&#039; ); ?&gt;&lt;\/label&gt;\t\n\t&lt;\/p&gt;\t\n\t&lt;p&gt;\n\t    &lt;input type=&quot;checkbox&quot; name=&quot;statut_diffucult_difficile&quot; id=&quot;statut_diffucult_difficile&quot; value=&quot;difficile&quot; &lt;?php echo ( statut_diffucult_get_meta( &#039;statut_diffucult_difficile&#039; ) === &#039;difficile&#039; ) ? &#039;checked&#039; : &#039;&#039;; ?&gt;&gt;\n\t\t&lt;label for=&quot;statut_diffucult_difficile&quot;&gt;&lt;?php _e( &#039;Difficile&#039;, &#039;crio-child&#039; ); ?&gt;&lt;\/label&gt;\t\n\t&lt;\/p&gt;\t\n\t&lt;p&gt;\n\t    &lt;input type=&quot;checkbox&quot; name=&quot;statut_diffucult_expert&quot; id=&quot;statut_diffucult_expert&quot; value=&quot;expert&quot; &lt;?php echo ( statut_diffucult_get_meta( &#039;statut_diffucult_expert&#039; ) === &#039;expert&#039; ) ? &#039;checked&#039; : &#039;&#039;; ?&gt;&gt;\n\t\t&lt;label for=&quot;statut_diffucult_expert&quot;&gt;&lt;?php _e( &#039;Expert&#039;, &#039;crio-child&#039; ); ?&gt;&lt;\/label&gt;\t\n\t&lt;\/p&gt;\n&lt;?php\n}\n\nfunction statut_diffucult_save( $post_id ) {\n\tif ( defined( &#039;DOING_AUTOSAVE&#039; ) &amp;&amp; DOING_AUTOSAVE ) return;\n\tif ( ! isset( $_POST[&#039;statut_diffucult_nonce&#039;] ) || ! wp_verify_nonce( $_POST[&#039;statut_diffucult_nonce&#039;], &#039;_statut_diffucult_nonce&#039; ) ) return;\n\tif ( ! current_user_can( &#039;edit_post&#039;, $post_id ) ) return;\n\n\tif ( isset( $_POST[&#039;statut_diffucult_facile&#039;] ) )\n\t\tupdate_post_meta( $post_id, &#039;statut_diffucult_facile&#039;, esc_attr( $_POST[&#039;statut_diffucult_facile&#039;] ) );\n\telse\n\t\tupdate_post_meta( $post_id, &#039;statut_diffucult_facile&#039;, null );\n\t\n\t\n\tif ( isset( $_POST[&#039;statut_diffucult_intermdiaire&#039;] ) )\n\t\tupdate_post_meta( $post_id, &#039;statut_diffucult_intermdiaire&#039;, esc_attr( $_POST[&#039;statut_diffucult_intermdiaire&#039;] ) );\n\telse\n\t\tupdate_post_meta( $post_id, &#039;statut_diffucult_intermdiaire&#039;, null );\n\t\n\t\n\tif ( isset( $_POST[&#039;statut_diffucult_difficile&#039;] ) )\n\t\tupdate_post_meta( $post_id, &#039;statut_diffucult_difficile&#039;, esc_attr( $_POST[&#039;statut_diffucult_difficile&#039;] ) );\n\telse\n\t\tupdate_post_meta( $post_id, &#039;statut_diffucult_difficile&#039;, null );\n\t\n\tif ( isset( $_POST[&#039;statut_diffucult_expert&#039;] ) )\n\t\tupdate_post_meta( $post_id, &#039;statut_diffucult_expert&#039;, esc_attr( $_POST[&#039;statut_diffucult_expert&#039;] ) );\n\telse\n\t\tupdate_post_meta( $post_id, &#039;statut_diffucult_expert&#039;, null );\n\t\n\t\n}\nadd_action( &#039;save_post&#039;, &#039;statut_diffucult_save&#039; );<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p><a class=\"more\" href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_post_meta\/\" target=\"_blank\" rel=\"noopener\">Learn more about the get_post_meta function<\/a><\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"This tutorial will help you learn how to create a metabox allowing users to collect data from a Post or custom Post Type. In the example below, we will use<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/build-and-display-a-wordpress-metabox\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4564,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[318],"class_list":["post-6245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-functions"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=6245"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6245\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4564"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}