dongnaosuan5407 2016-01-29 17: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-29 19: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');
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口
  • ¥15 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
  • ¥20 谁刷目标页面的uv记录器上数据,数据只记录跳转的数值
  • ¥30 数据库软件的安装方法
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部