Custom Meta Field – Youtube embed

I created a custom meta field and I want to use it to embed a youtube video into the post. It works but when I post a code like this:

<iframe width="640" height="360" src="//www.youtube.com/embed/5Krz-dyD-UQ?feature=player_detailpage" frameborder="0" allowfullscreen></iframe>

Read More

It doesn’t save. If I put a link or any other plain text it does save.

Any tips to make it work with iframe codes?

CODE:

<?php

add_action( 'add_meta_boxes', 'url_meta_box' );
function url_meta_box()
{
    add_meta_box( 'url-box', 'Additional Resources', 'url_meta_box_func', 'post', 'normal', 'high' );
}

function url_meta_box_func( $post )
{
    $values = get_post_custom( $post->ID );
    $video1 = isset( $values['video_1'] ) ? esc_attr( $values['video_1'][0] ) : '';

    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
    <p>
        <input type="text" name="video_1" placeholder="Youtube Embed Code" size="75" id="video_1" value="<?php echo $video1; ?>" />
    </p>
    <?php   
}


add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;

    // now we can actually save the data
    $allowed = array( 
        'a' => array( // on allow a tags
            'href' => array() // and those anchords can only have href attribute
        )
    );

    // Probably a good idea to make sure your data is set

    if( isset( $_POST['video_1'] ) )
        update_post_meta( $post_id, 'video_1', wp_kses( $_POST['video_1'], $allowed ) );

}
?>

Related posts

1 comment

  1. Maybe it’s better to use a textarea instead of an input field. And of course $allowed doesn’t contain the iframe tag. And for sure you will be not able delete the saved video_1

    Too many things not really good in this code.

    1)

    $values = get_post_custom( $post->ID );
    $video1 = isset( $values['video_1'] ) ? esc_attr( $values['video_1'][0] ) : '';
    

    replace with

    $video1 = get_post_meta($post->ID, 'video_1', true);
    

    2)

    <input type="text" name="video_1" placeholder="Youtube Embed Code" size="75" id="video_1" value="<?php echo $video1; ?>" />
    

    replace with

        <textarea  name="video_1" placeholder="Youtube Embed Code" style="width:100%" id="video_1"> <?php echo esc_html($video1); ?></textarea>
    

    3)

    if( isset( $_POST['video_1'] ) )
    

    replace with

    if( isset( $_POST['video_1'] ) && !empty($_POST['video_1']) )
        update_post_meta( $post_id, 'video_1', wp_kses( $_POST['video_1'], $allowed ) );
    else 
        delete_post_meta( $post_id, 'video_1');
    

    4)

    // now we can actually save the data
    $allowed = array( 
        'a' => array( // on allow a tags
            'href' => array() // and those anchords can only have href attribute
        )
    );
    

    replace with

    // now we can actually save the data
    $allowed = array( 
        'iframe' => array( // on allow a iframe
        )
    );
    

Comments are closed.