【问题标题】:Where to put custom PHP and JQuery code on Wordpress?将自定义 PHP 和 JQuery 代码放在 Wordpress 的什么位置?
【发布时间】:2016-05-07 21:53:26
【问题描述】:

我对 Wordpress 很陌生,但对 Web 开发并不陌生。

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

我已经有一个 Youtube API Data V3 的 API KEY 和正确的 URL 来播放我的视频。另外,我读到了这个很棒的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);

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

【问题讨论】:

    标签: php jquery wordpress youtube


    【解决方案1】:

    通常的位置是在您的主题或子主题中的functions.php(建议更新将覆盖您主题中的自定义代码)或您自己创建的插件(pluginname.php)。

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

    如果有任何错误,请在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');
    

    【讨论】:

    • 谢谢!真的很有帮助和完整的答案。顺便说一句,在 V3 上,我们可以访问 nextPageToken 属性以获得分页效果。
    • 我不太清楚你的意思,代码会读取整个 json 输出。如果您愿意,您可以将令牌保存到 postmeta 中,但它与 wp 无关,这没关系,因为 wp 在帖子列表上进行分页。你接受这个答案还是我错过了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    相关资源
    最近更新 更多