-1

I add videos to my WordPress posts' video format by using custom fields. I was wondering if there is any way to create a meta box in post editor (like excerpt or something) for that specific custom field. Just a text area to add iframe code. For example the custom field is a embed-video.

1 Answer 1

2

This will create a metabox for you to enter a video code.

    //Creating a MetaBox for Posts to enter Video Code.
add_action('add_meta_boxes','video_meta_box');
function video_meta_box(){
    add_meta_box('video_box_id', 'Enter Video ' , 'video_box_cb','post','normal','default');
}
function video_box_cb($post){
    $value = get_post_meta($post->ID,'video_box',true);

    echo '<textarea rows="4" cols="50" id="video_box", name="video_box">';
        echo $value;
    echo '</textarea>';
}

add_action('save_post','save_video_box');
function save_video_box($post_id){
    $box_data = $_POST['video_box'];
    update_post_meta($post_id,'video_box',$box_data);
}

Use this code to render this code within your theme.

<?php
    $custom_values = get_post_custom($post->ID);
    echo '<p> Video CODE: '.$custom_values['video_box'][0].'</p>';
?>
5
  • Thanks!! It worked! Can you tell me why there is [0] after ['video_box'] Feb 19, 2014 at 13:04
  • 1
    Glad it worked... Please mark it as accepted answer on the left side of the answer. [0] signifies first element of the array. You can do var_dump($custom_values['video_box']); to see what's in the array and what to be retrieved. Feb 19, 2014 at 13:31
  • I marked this thread as resolved. However, is it possible to do this with a custom field? Feb 19, 2014 at 17:07
  • Yes, this can be done with a custom field too. But since you need to have dedicated box for entering come custom then making a metabox is a good idea. Feb 20, 2014 at 3:44
  • Yes, it is, but there are more than 100 posts already with that custom_field so creating a meta box for field will be a better idea. Plus, I'll also learn how to do that ;) Please explain that too.. Feb 20, 2014 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.