What you want is a custom meta box.
Searching this site will give you plenty of examples as well, but here’s a brief run down.
You hook into add_meta_boxes
and call, appropriately, add_meta_box
.
<?php
add_action('add_meta_boxes', 'wpse87567_add_box');
function wpse87567_add_box($post_type)
{
// $post_type the "page" on which the `add_meta_boxes` action is being fired
// It could be commends or could be a post type. It's really only
// useful if you want to dynamically add a meta box everywhere.
add_meta_box(
'wpse87567-box', // the meta box ID, can be anything
__('A Meta Box', 'wpse'), // your meta box title
'wpse87567_box_callback', // the callback, the function that spits our your meta box contents (eg. fields)
'landing_page', // the post type to which you wish to add the meta box
'side', // where should you put it? side, normal, or advanced
'low' // how important is it? high, core, default, or low
);
}
function wpse87567_box_callback($post)
{
// post is the current page's post object, use it to fetch your additional
// meta data via `get_post_meta` or the like.
// spit out your HTML fields here
}
To save things, you hook into save_post
and check to make sure you’re not doing an autosave, the post types match up, the nonces check out, and the the current user can actually edit the post
<?php
add_action('save_post', 'wpse87567_save', 10, 2);
function wpse87567_save($post_id, $post)
{
// make sure we aren't doing an autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// check if your post types match: do you need to save...
if ('landing_page' !== $post->post_type) {
return;
}
// If you used a nonce above, and you should have, check it here.
// See if the current user can edit the post
// `edit_post` should be whatever capability your post type was
// registered with. Maybe be `edit_page` or `edit_landing_page`, etc
if (!current_user_can('edit_post', $post_id)) {
return;
}
// if you're here, save stuff.
if (!empty($_POST['some_field_from_the_callback'])) {
update_post_meta(
$post_id,
'_your_meta_key',
esc_attr(strip_tags($_POST['some_field_from_the_callback']))
);
} else {
delete_post_meta($post_id, '_your_meta_key');
}
}