dongnaosuan5407 2016-01-30 01:04
浏览 102
已采纳

在Wordpress上放置自定义PHP和JQuery代码的位置?

I am really new to Wordpress but not to web development.

I am creating a wordpress based website, I have a youtube channel with more than 450 videos, and I would like to create a post for each one of them.

I already have an API KEY for Youtube API Data V3 and the right url to bring my videos. Also, I read about this great wp_insert_post that seems to do exactly what I need to do.

What I want to know is where to put my code? It will be very simple, something like: Clientside:

$.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);
    });
});

Server Side:

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

This will be a one-time job, so I am trying to implement this quick client-server solution.

  • 写回答

1条回答 默认 最新

  • dsi36131 2016-01-30 03:08
    关注

    the usual place is in functions.php in your theme or child theme (recommended as updates will overwrite custom code in your theme) or a plugin you create yourself (pluginname.php).

    I have put the best way to achieve this below, its not client side at all but I believe this will suit you better. Basically on every page load the code will run and upload the next 50 videos (if timing out reduce the figure below -- see comments) (you should remove the code when all videos are uploaded).

    Also excuse me if there are any errors, typed this out on a txt file. Also i dont know the structure of the json returned, i dont have a api key to check, you will need to update the correct structure in the for loop to get the data you want...

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan