如何在工具集metabox中使用按钮点击保存-发布之前打印YouTube API数据



我在post.php中创建了一个带有'import'按钮的metabox。此外,准备youtube api数据代码。但我需要,'当我点击'导入'按钮运行YouTube API数据功能和值打印在工具集自定义字段metabox之前,我发布或起草的帖子。之后,我检查数据是正确的,然后将在wordpress php中发布帖子。

function add_your_meta_box()
{

add_meta_box('import-data', 'YT Import', 'function_of_metabox', 'movie', 'side', 'high');
}

add_action('add_meta_boxes', 'add_your_meta_box'); 

function function_of_metabox()
{?>
<input type="submit" class="button button-primary button-large" value="Import" id="Import"/>
<?php }

按钮元框

function my_add_custom_fields1(){
$post_id = get_the_ID();   
$meta2 = get_post_status($post_id);
$meta3 =   get_post_type( $post_id );
$meta1 = get_post_meta(get_the_ID(), 'wpcf-song-duration', true);  
$ytURL = get_post_meta(get_the_ID(), 'wpcf-song-av', true);
// video id from url
$YouTubeCheck = preg_match('%(?:youtube(?:-nocookie)?.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu.be/)([^"&?/ ]{11})%i', $ytURL, $Data);
If($YouTubeCheck){
$youtube_id1 = $Data[1];
}
// video json data
$dur1 = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$youtube_id1&key=AIzaSyAbFclaJBklZ53t_UD-Xz85FbWsfD37sX0");
$duration_key1 = json_decode($dur1, true);
$duration1 = $duration_key1['items'][0]['contentDetails']['duration'];
$start = new DateTime('@0');
$start->add(new DateInterval($duration1));
$h11 = $start->format('H:i:s');
// print the value toolset meta box using print_r $h11;
}
print value toolset custom field meta box 
[toolset custom field meta box][2]

我找到了编码的解决方案

<script>
jQuery('.Import').live('click', function(e) {
e.preventDefault();
jQuery('#save').click();
});

</script>

在同一文件中添加jquery脚本

$ytURL = get_post_meta(get_the_ID(), 'wpcf-song-av', true);

改变

$ytURL = $_POST['ytext'];

添加文本字段

<input id="textbox_id" name="ytext" placeholder="YT ID here" type="text" />
<input type="submit"  name="Import" class="Import" id="Import"  />

最新更新