duankuang7928 2016-09-09 17:52
浏览 30

Wordpress多站点Ajax错误

I have a WordPress multisite set up. Everything works fine except my ajax calls return 500s, this is something that I'm pretty sure started when I converted to multisite.

Snippet of Ajax Code that makes the call:

    function ajaxShowComments(post,numberToLoad){
    $.ajax({
        url: eventComments.commenturl,
        type: 'post',
        data:{
            'postId': post,
            'comment-offset': offset,
            'count': numberToLoad
        },
        success: function(result){
            var arr = JSON.parse(result);

            if( arr.length == 0 ) {
                $('.readmore').css('display', 'none');
            if( arr.length == 0 && offset == 0 )
                $('.latestShows').append('<div id="nocomment"> No one has left a comment for this show yet! </div> <button class="first-comment-btn">Be The First!</button>')

                $('.first-comment-btn').click(function () {
                    showModal();
                });
            }
            arr.forEach(function (item) {
                $('.readmore').before('<div class="lazy-comment">' + item + '</div>');
            });
            offset += numberToLoad;
        }
    });
}

Snippet of my themes functions.php, which creates the URL for the Ajax call:

function pebl_culture_assets() {
 // Enqueue/register other styles/scripts etc... 

wp_localize_script('pebl-main', 'eventComments', array(
        'commenturl' => get_template_directory_uri().'/getcomments.php')
);
}

add_action( 'wp_enqueue_scripts', 'pebl_culture_assets' );

This works fine, script in html head looks like this:

//script tags omitted 
var eventComments = {"commenturl":"http:\/\/peblarts.com\/{{SUBSITE NAME}}\/wp-content\/themes\/peblculture\/getcomments.php"};

So far so good, script loads the URL which is passed to the AJAX call (post, offset, numberToLoad all defined in js). The call is definitely made to getcomments.php which looks like this:

<?php
require_once("../../../wp-load.php");

$cmts = get_comments( array( 'post_id' => $_POST['postId'] , 'status' => 'approve') );

$comments = [];
$count = $_POST['count'];

if( !empty($cmts) ) {
    for ($i = $_POST['comment-offset']; $i < $_POST['comment-offset'] + $count; $i++ ) {
        if( !empty($cmts[$i]) )
            array_push($comments, $cmts[$i]->comment_content);
    }
    echo json_encode($comments);
}else{
    echo json_encode(array());
}

But it seems it breaks when trying to get the comments, very annoying, I have tried editing file permissions, tried changing the URL to explicitly point to where I know getcomments.php lives but that does not fix it. 500s all day long.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
    • ¥15 安装svn网络有问题怎么办