如何使用按钮单击YouTube API数据在Save-Post之前单击工具集Metabox

发布于 2025-01-26 08:15:18 字数 1775 浏览 3 评论 0原文

我在post.php中使用“导入”按钮创建一个metabox。另外,准备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]

i create a metabox with 'import' button in post.php. Also, ready for youtube api data code. but i need, 'when i click 'import' button to run youtube api data function and the values are print in toolset custom fields metabox before i publish or draft the post. after, i check the data is correct then will publish the post in 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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2025-02-02 08:15:18

我发现编码的解决方案

<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"  />

i find the solution of my coding

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

add the jquery script in same file.

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

change to

$ytURL = $_POST['ytext'];

add text field

<input id="textbox_id" name="ytext" placeholder="YT ID here" type="text" />
<input type="submit"  name="Import" class="Import" id="Import"  />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文