Custom Meta Field – Youtube embed

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
    )
);

Leave a Comment