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>
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 ) );
}
?>
Maybe it’s better to use a
textarea
instead of aninput
field. And of course$allowed
doesn’t contain theiframe
tag. And for sure you will be not able delete the savedvideo_1
…Too many things not really good in this code.
1)
replace with
2)
replace with
3)
replace with
4)
replace with