php - 在哪里将自定义PHP和JQuery代码放在Wordpress上?

标签 php jquery wordpress youtube

我真的是Wordpress的新手,但不是Web开发的新手。

我正在创建一个基于wordpress的网站,我有一个YouTube channel ,其中包含450多个视频,我想为每个视频创建一个帖子。

我已经有适用于Youtube API Data V3的API KEY和正确的网址来带我的视频。此外,我阅读了有关此功能强大的wp_insert_post的信息,该文件似乎恰好满足了我的需要。

我想知道的是,我的代码放在哪里?这将非常简单,例如:
客户端:

$.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=UUbRrCTEldKci2yWosUukSQQ&key=APIKEY');
.success(function(data){
    data.forEach(function(vid){
        $.post('myPhpPostInserter.php', vid);
    });
});

服务器端:
<?php
$myPost['title'] = $_POST['vid_title'];
//some more mapping on the $myPost array...
wp_insert_post($myPost);

这将是一项一次性的工作,因此我正在尝试实现这种快速的客户端-服务器解决方案。

最佳答案

通常放在主题或子主题中的functions.php中(建议进行更新,因为更新将覆盖主题中的自定义代码)或您自己创建的插件(pluginname.php)。

我在下面提出了实现此目标的最佳方法,它根本不是客户端,但是我相信这会更适合您。基本上,在每次加载页面时,代码都会运行并上传接下来的50个视频(如果超时,请减少下图-参见评论)(所有视频都上传后,您应该删除代码)。

如果有任何错误,也请原谅,请在txt文件中输入。另外我不知道返回的json的结构,我没有api键来检查,您将需要在for循环中更新正确的结构以获取所需的数据...

function upload_videos(){


    if(!get_option( 'vid_count' ) ){
        $x=0;
        update_option( 'vid_count', $x );
    } else {
        $x= (int) get_option('vid_count');
    }

    //might as well cache the file to speed things up
    if( !file_exists ( 'videos.json' )){

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=5000&playlistId=UUbRrCTEldKci2yWosUukSQQ&key=APIKEY');
        $json = curl_exec($ch);
        curl_close($ch);

        $videos = fopen("videos.json", "w") or die("Unable to open file!");
        fwrite($videos, $json);
        fclose($videos);
        unset($videos);
    }

    $videos =  fopen("videos.json", "r");
    $videos = json_decode($videos);


    //get the structure of the array here and update the foreach loop to the right place in the object...
    //remove this code when ready.....
    var_dump($videos);
    exit;
    //end remove.....


    /*
        remember to update to the correct values....
        we will do 50 at a time
    */
    $max= $x+50; // adjust 50 downwards if needed

    if($max > count( $videos['list'] ) )
        $max= count( $videos['list'] );

    if($max > $x)
        return;


    update_option('vid_count', $max );

    for($v=$x; $v<=$max; $v++){

        $id= wp_insert_post(
        array(
            'post_status'=>'publish',
            'post_type'=>'video', //higher recommended to create a cpt and post template for this..
            'post_title'=> $videos['list'][$v]->title, // sample --- dont know the format of the returned object
            'content'=> 'whatever you want or html for the video, etc................'
        ));

        //save the url into the metadata of the post....we can use this in templates to show the video....
        update_post_meta($id, '_vid_url', $videos['list'][$v]->video_url);// sample --- dont know the format of the returned object

    }

}

add_action('init', 'upload_videos');

关于php - 在哪里将自定义PHP和JQuery代码放在Wordpress上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35096784/

相关文章:

jquery - 使用简写 if 更改属性时遇到问题

jquery - 显示按钮的问题

javascript - Woocommerce 通过 JS 更改产品变体

php - MySQL 循环或嵌套查询

javascript - jQuery 延迟/ promise

php - 从 PHP 向 MySQL 插入 BLOB 数据时,我看不到准确的数据类型

wordpress - 完整的 Magento/Wordpress 集成

php - 如何在 woocommerce ajax add_to_cart 之后运行我的自定义 ajax?

php - Magento 1.8.0 和 1.8.1 特价发售

php - 仅使用 301 重定向重定向 WordPress 主页