我在自定义帖子上创建了一个自定义字段。下面是我用于自定义字段的代码。
<?php
function wpt_add_thirdpartyvideo_metaboxes() {
add_meta_box(
\'wpt_thirdpartyvideo_extra\',
\'Information\',
\'wpt_thirdpartyvideo_extra\',
\'partyvideo\',
\'normal\',
\'default\'
);
}
add_action( \'add_meta_boxes\', \'wpt_add_thirdpartyvideo_metaboxes\' );
function wpt_thirdpartyvideo_extra() {
global $post;
// Nonce field to validate form request came from current site
wp_nonce_field( basename( __FILE__ ), \'thirdpartyvideo_extra\' );
// Output the field
echo \'<table width="100%" border="0" cellspacing="0" cellpadding="10">
<tr>
<td>Video Script here </td>
<td>
<div class="form-check">
<textarea name="thirdpartyvideoscript">\' . esc_textarea( get_post_meta( $post->ID, \'thirdpartyvideoscript\', true ) ) . \'</textarea>
</div>
</td>
</tr>
</table>\';
}
function wpt_save_thirdpartyvideo_meta( $post_id, $post ) {
if ( ! current_user_can( \'edit_post\', $post_id ) ) {
return $post_id;
}
if ( ! isset( $_POST[\'thirdpartyvideoscript\'] ) ) {
return $post_id;
}
$events_meta[\'thirdpartyvideoscript\'] = esc_textarea( $_POST[\'thirdpartyvideoscript\'] );
foreach ( $events_meta as $key => $value ) :
if ( \'revision\' === $post->post_type ) {
return;
}
if ( get_post_meta( $post_id, $key, false ) ) {
update_post_meta( $post_id, $key, $value );
} else {
add_post_meta( $post_id, $key, $value);
}
if ( ! $value ) {
delete_post_meta( $post_id, $key );
}
endforeach;
}
add_action( \'save_post\', \'wpt_save_thirdpartyvideo_meta\', 1, 2 );
?>
我已经尝试添加一些虚拟内容,它正在工作。现在我必须添加第三方视频代码,即脚本和HTML标记,如下面的第三方示例所示。
<script src="//fast.wistia.com/embed/medias/s3lqfi0zn7.jsonp" async></script>
<script src="//fast.wistia.com/assets/external/E-v1.js" async></script>
<div class="wistia_embed wistia_async_s3lqfi0zn7" style="height:349px;width:620px"> </div>
如果我添加上述代码,那么我将得到以下输出
<script src="//fast.wistia.com/embed/medias/s3lqfi0zn7.jsonp" async></script>
<script src="//fast.wistia.com/assets/external/E-v1.js" async></script>
<div class="wistia_embed wistia_async_s3lqfi0zn7" style="height:349px;width:620px">&nbsp;</div>