{"id":822,"date":"2016-01-14T10:24:08","date_gmt":"2016-01-14T01:24:08","guid":{"rendered":"http:\/\/n8finch.com\/?p=822"},"modified":"2019-06-30T07:03:37","modified_gmt":"2019-06-29T22:03:37","slug":"custom-meta-boxes-and-post-meta","status":"publish","type":"post","link":"https:\/\/n8finch2024.local\/custom-meta-boxes-and-post-meta\/","title":{"rendered":"This Week I Learned: Custom Meta Boxes and Post Meta"},"content":{"rendered":"
“Can we have a l way to link a slider image to a specific page?” my client asked.<\/p>\n
“Sure, no problem,” I said.<\/p>\n
Now, I had never done this before, but it seemed easy enough to do. In my mind, I pictured the solution:<\/p>\n
Well.. it wasn’t quite as straightforward as that, but not too far off. Thanks to Google, the WordPress Codex<\/a><\/strong> (bless you!), and a pointer from the awesome folks at ZigZagPress.com<\/a><\/strong> (thanks Joe!), I was off to the races.<\/p>\n Here’s what I did…<\/p>\n I’m still a little murky on the difference between custom fields and custom metaboxes, as there’s definitely some overlap, but the long and short of it is:<\/p>\n Custom fields are key\/value pairs for storing data. Metaboxes are structures that contain custom fields, or at least, a place to store custom data. Metaboxes can be styled and dragged around on a post type view.<\/p>\n<\/blockquote>\n Great! So, I set to work on adding my first real like custom metabox!<\/p>\n I headed over to the WordPress Codex and grabbed the code for adding a metabox<\/a><\/strong>. After check the parameters, and deciding I wanted the box on the sidebar high up on the post page, I ended up with this basic function and hook:<\/p>\n If you look at the comments in the code, you can see the main pieces I changed and why. Parameter values are listed on the Codex page.<\/p>\n Implementing this code in my functionality plugin yielded the desired results:<\/p>\nAdding the Metabox<\/h2>\n
\n
\/*\r\n * Adds Custom Meta Box for Slider Hyperlink\r\n -------------------------------------------*\/\r\n\r\n\/**\r\n * Adds a box to the main column on the Post and Page edit screens.\r\n *\/\r\nfunction n8f_add_meta_box() {\r\n\r\n $screens = array( 'slide' ); \/\/determines what post type this will show up on\r\n\r\n foreach ( $screens as $screen ) {\r\n\r\n add_meta_box(\r\n 'n8f_sectionid',\r\n __( 'Slide hyperlink ', 'n8f_textdomain' ), \/\/'Slide hyperlink' is the title of the metabox\r\n 'n8f_meta_box_callback',\r\n $screen,\r\n 'side', \/\/where I want the metabox to be, main or side content\r\n 'high' \/\/priority of the metabox, we want that guy on top!\r\n );\r\n }\r\n}\r\nadd_action( 'add_meta_boxes', 'n8f_add_meta_box' );\r\n\r\n<\/pre>\n