{"id":743,"date":"2008-02-25T22:51:38","date_gmt":"2008-02-25T20:51:38","guid":{"rendered":"http:\/\/planetozh.com\/blog\/2008\/02\/wordpress-snippet-add_meta_box\/"},"modified":"2008-02-25T22:51:38","modified_gmt":"2008-02-25T20:51:38","slug":"wordpress-snippet-add_meta_box","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2008\/02\/wordpress-snippet-add_meta_box\/","title":{"rendered":"WordPress Snippet: add_meta_box()"},"content":{"rendered":"<p>To add custom content to the &quot;Write&quot; admin pages (where you write posts, pages or links), WordPress 2.5 introduces a new function set: add_meta_box()<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/02\/add_meta_box.gif\" alt=\"add_meta_box\" class=\"imgcenter\" \/><\/p>\n<p>Simple plugin example :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">&lt;?php\r\n\/*\r\nPlugin Name: Example: Add Meta Box\r\nPlugin URI: #\r\nDescription: Simple example showing how to add a &quot;meta box&quot; in WP 2.5\r\nVersion: 0.0\r\nAuthor: Ozh\r\nAuthor URI: http:\/\/planetozh.com\/blog\/\r\n*\/\r\n\r\n\/\/ This function tells WP to add a new &quot;meta box&quot;\r\nfunction add_some_box() {\r\n\tadd_meta_box(\r\n\t\t&#039;ozh&#039;, \/\/ id of the &lt;div&gt; we&#039;ll add\r\n\t\t&#039;My Box&#039;, \/\/title\r\n\t\t&#039;add_something_in_the_box&#039;, \/\/ callback function that will echo the box content\r\n\t\t&#039;post&#039; \/\/ where to add the box: on &quot;post&quot;, &quot;page&quot;, or &quot;link&quot; page\r\n\t);\r\n}\r\n\r\n\/\/ This function echoes the content of our meta box\r\nfunction add_something_in_the_box() {\r\n\techo &quot;I&#039;m living in a box&quot;;\r\n}\r\n\r\n\/\/ Hook things in, late enough so that add_meta_box() is defined\r\nif (is_admin())\r\n\tadd_action(&#039;admin_menu&#039;, &#039;add_some_box&#039;);\r\n\r\n?&gt;<\/code><\/pre><\/div>\n<p>The catch here is to hook the function that add our &quot;meta box&quot; (here: add_some_box()) late enough so that the function it needs, add_meta_box(), has been defined. Hooking on &#39;admin_menu&#39; is fine.<\/p>\n<p>If you want something compatible with both WordPress 2.3 and 2.5, you will need something like the following:<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-2\"><pre class=\"language-php line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-php\">if (function_exists(&#039;add_meta_box&#039;) {\r\n\t\/\/ 2.5 style\r\n} else {\r\n\t\/\/ 2.3\r\n}<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>To add custom content to the &quot;Write&quot; admin pages (where you write posts, pages or links), WordPress 2.5 introduces a new function set: add_meta_box() Simple plugin example : The catch here is to hook the function that add our &quot;meta box&quot; (here: add_some_box()) late enough so that the function it needs, add_meta_box(), has been defined. Hooking on &#39;admin_menu&#39; is fine.\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_ozhmd_markdown":"","footnotes":""},"categories":[21],"tags":[282,245,277],"class_list":["post-743","post","type-post","status-publish","format-standard","hentry","category-published","tag-add_meta_box","tag-wordpress","tag-wordpress-snippet"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=743"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/743\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}