Categories
Wordpress

Custom Boxes in Posts

You can create a custom box in post/page/custom-post-type by adding a couple of actions and telling it to trigger for the post-type.

1. add_action( ‘add_meta_boxes’, ‘people_add_custom_box’ ); // this hooks into the ‘add_meta_boxes’ action, and calls the function people_add_custom_box

2.     function people_add_custom_box() {
add_meta_box(
‘people-quote’,
__( ‘Quote’, ‘quote_textdomain’ ),
‘people_inner_custom_box’,
‘people’
);
Each add_meta_box creates a separate box, in this case it calls function people_inner_custom_box() to generate its HTML

3. function people_inner_custom_box($post). Use this to generate the form fields. You can use a nonce to prevent accidental posting

$collection_link = get_post_meta($post->ID,’collection_link’,true);
$template = get_post_meta($post->ID,’template’,true);
$video = get_post_meta($post->ID,’video’,true);
?>

function designer_page_template($post) {
$collection_link = get_post_meta($post->ID,’collection_link’,true);
$template = get_post_meta($post->ID,’template’,true);
$video = get_post_meta($post->ID,’video’,true);
?>
Page Type: <select name=”template”>
<option value=”landing” <?php echo ($template==”landing”) ? ‘selected=”selected”‘:”;?>>Designer Landing Page</option>
<option value=”history” <?php echo ($template==”history”) ? ‘selected=”selected”‘:”;?>>History Page</option>
<option value=”inspiration” <?php echo ($template==”inspiration”) ? ‘selected=”selected”‘:”;?>>Inspiration Page</option>
<option value=”video” <?php echo ($template==”video”) ? ‘selected=”selected”‘:”;?>>Video Page</option>
</select><br />
Collection Link: <input type=”text” name=”collection_link” id=”collection_link” value=”<?php echo $collection_link;?>” style=”width:400px”/><br />
Video embed code:<br />
<textarea style=”width:500px;height:100px;” name=”video”><?php echo stripslashes($video);?></textarea>
<?
}

4. add_action( ‘save_post’, ‘people_save_postdata’ ); // hooks into save_post and calls function people_save_postdata(). You can then use update_post_meta() to store the data.

5.function people_save_postdata($post_id) {
if ( defined( ‘DOING_AUTOSAVE’ ) && DOING_AUTOSAVE )
return;
$address = $_POST[‘address’];
if ($address) update_post_meta($post_id,’address’,$address);

}

Leave a Reply