如何在保存发布之前使用单击按钮将 youtube api 数据打印在工具集元框中

问题描述 投票:0回答:1

我在 post.php 中创建了一个带有“导入”按钮的元框。另外,准备好 youtube api 数据代码。但我需要,“当我单击“导入”按钮运行 youtube api 数据函数时,在发布或起草帖子之前,这些值将打印在工具集自定义字段元框中。之后,我检查数据是否正确,然后将在 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 }

button meta box

    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]
php wordpress buttonclick meta-boxes devtoolset
1个回答
0
投票

我找到了我的编码的解决方案

<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"  />
© www.soinside.com 2019 - 2024. All rights reserved.