2011-12-08 6 views
0

저는 WordPress 프로젝트에서 일하고 있습니다. 아약스와 함께 실망한 이슈가 있습니다.wordpress admin-ajax.php issues

나는 getHistoricalTransFunctions.php를 호출 한 php 스크립트로 WordPress 페이지에서 ajax 호출을 설정하려고합니다. getHistoricalTransFunctions.php는 함수로 가득 찬 파일을 포함하고 필요한 함수를 실행합니다. 그러면 필요한 함수가 응답을 출력하고 응답을 표시하는 자바 스크립트 코드로 다시 전송됩니다. catch는 NEEDS를 특정 wordpress 함수를 호출하기 때문에 WordPress 환경에 있기 위해 호출하려고하는 함수입니다.

나는 약간 연구를했고 wordpress가 admin-ajax.php에서 ajax 처리기를 제공한다는 것을 발견했다. 내가 포함 튜토리얼의 숫자 따랐습니다 :

http://codex.wordpress.org/AJAX_in_Plugins/

http://www.1stwebdesigner.com/css/implement-ajax-wordpress-themes/

http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/

나는이 모든,하지만 여전히 내가 갖는 어떤 이유에서 수행 한을은 "-1 "admin-ajax.php 페이지의 응답. 나는 그것을 추적하고 그것이 is_user_logged_in() 함수에서 시작되었다는 것을 발견했다. 분명히 wordpress는 내 사용자가 로그인 한 것으로 생각하지 않으므로 해당 코드 블록에서 오류가 발생합니다. 여기에 내 코드의 일부는 다음과 같습니다

이 내 자바 스크립트 호출입니다 :

$('button#RunReportButton2').click(function() { 
    $('#transactionContainer2').html("<img src='<?php echo $RootDomain; ?>/wp-content/themes/test-client/images/ajax-loader.gif' id='ajaxloader' style='margin: 170px auto auto 340px;' />"); 
    var fromDate2 = $('#fromDate2').val(); 
    var toDate2 = $('#toDate2').val(); 
    $.ajax({ type: "POST", 
    url:ajaxurl, 
    type:'POST', 
    data: { action:"runReport2", 
      startingInt:"0", 
      fromDate:fromDate2, 
      toDate:toDate2 }, 
    success: function(html) { 
     $('#transactionContainer2').html(html); 
    } 
    }); 
    return false; 
}); 

내가 관리자 - ajax.php의 바닥이 추가 :

add_action(wp_ajax_nopriv_runReport2, runReport2); 
add_action(wp_ajax_runReport2, runReport2); 
다음

내 실제 PHP 함수이 관리자 - ajax.php 내가해야 할 일을 수행하는 가장 좋은 방법 인 경우

function runReport2() { 
    include("$RootDomain/wp-content/themes/test-client/reports/historicalTransFunctions.php"); 

    $startingIndex = $_POST['startingInt']; 
    //$startingIndex = 0; 
    $fromDate = $_POST['fromDate']; 
    //$fromDate = "2011-02-11"; 
    $toDate = $_POST['toDate']; 
    //$toDate = "2011-12-05"; 

    // post variable sanitization 
    if(!is_numeric($startingIndex)) { 
    printHistoricalTransactions($token, $client, 0); 
    die(); 
    } 

    if($startingIndex <= 0) { 
    printHistoricalTransactions($token, $client, 0); 
    die(); 
    } 

    // match date 
    $dateregex = '/^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/'; 

    if($toDate != "" && $fromDate != "") { 
    if(preg_match($dateregex, $fromDate) && preg_match($dateregex, $toDate)) 
     printHistoricalTransactions($token, $client, $startingIndex, $fromDate, $toDate); 
    } else { 
    printHistoricalTransactions($token, $client, $startingIndex); 
    } 
    die(); 
} 

가 궁금 해요, 난 알거야 :라고이다 왜 이것이 작동하지 않는지 궁금합니다. 감사!

답변

3

핵심 파일을 수정해서는 안됩니다. admin-ajax.php에 ajax 함수를 추가 할 필요가 없습니다. add_action을 함수 바로 위에 놓기 만하면됩니다. 또한 add_action에는 작업 및 함수 이름에 따옴표가 없습니다 (여기에 코드를 게시 할 때 아마도 감시). 사용자가 로그인하지 않는

또한 자바 스크립트 ajaxurl 변수는 프론트 엔드에서 사용할 수 없습니다 당신은 당신의 js의 변수를 정의해야합니다. ajaxurl = 'http://your_site.com/wp-admin/admin-ajax.php'; I 출력을 사용해야 관리자 - ajax.php 자주

올바르게 작동하도록 버퍼링 :

+0

감사합니다. Chris. 도움이되었습니다. 내 문제가 내 runReport2() 함수에있는 $ RootDomain 변수를 사용하여 포함 시키려고했습니다. 변수가 잘못되어 경로를 하드 코드 했으므로 완벽하게 작동했습니다. 또한 admin-ajax.php 파일에서 모든 코드를 옮겼습니다. 이제는 functions.php에 있습니다. 팁 주셔서 감사합니다! – Erreth

+0

admin ajax URL을 하드 코딩하지 마십시오. 'admin_url ('admin-ajax.php')'와 같은 것을 사용하십시오. 그 외에는 "http :"를 하드 코딩하지 않고 기본 도메인을 변경해야합니다 (예 : 로컬 테스트). – Jake