自定义元字段-YouTube嵌入

时间:2013-10-18 作者:Jacob

我创建了一个自定义元字段,我想用它将youtube视频嵌入到帖子中。但当我发布这样的代码时:

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

它无法保存。如果我放置链接或任何其他纯文本,它会保存。

有什么建议吗iframe 代码?

代码:

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

}
?>

1 个回复
最合适的回答,由SO网友:Oleg Butuzov 整理而成

也许使用textarea 而不是input 领域当然还有$allowed 不包含iframe 标签当然,您将无法删除保存的video_1...

这段代码中有太多不太好的地方。

(1)

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

$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; ?>" />
替换为

    <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\'] ) )
替换为

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
    )
);
替换为

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

结束

相关推荐

使用PHP从PodPress获取URI

我们使用podpress作为播客播放器,但我们希望提供在没有播放器的页面上下载文件的选项。因此,我试图找出如何从元数据中提取URI。我是PHP新手,正在努力学习,所以我已经走到了这一步:<?php $custom_fields = get_post_custom(get_the_ID()); $my_custom_field = $custom_fields[\'_podPressMedia\']; foreach ( $my_custom_fie