2014-07-09 1 views
1

저는 AJAX를 통해 양식 데이터를 WordPress의 새 게시물을 작성하는 PHP 스크립트로 보내려고합니다. 내가 가지고있는 코드는 작동해야하는 것처럼 보이지만, 어떤 이유로 아무 일도 일어나지 않습니다.새 게시물을 작성하는 AJAX 양식 (WordPress)

양식은 매우 거대하다,하지만 난 지금 작업을 진행하게 노력하고있어 기본 정보는 다음과 같습니다

<form action="/create-event" method="POST" enctype="multipart/form-data" class="event-form"> 
    <input type="hidden" name="userID" id="userID" value="<?php echo get_current_user_id(); ?>" /> 

    <fieldset> 
     <label for="event-name">Event Name</label> 
     <input type="text" id="event-name" name="event-name" value="" required /> 
    </fieldset> 

    <input type="text" id="event-main-cat" name="event-main-cat" class="main-cat" required /> 
    <input type="text" id="sub-cat" name="sub-cat" class="sub-category-input" value="" /> 

</form> 

AJAX :

(function($){ 
$(document).ready(function() { 


$('input[type=button]').on('click', function() { 

    if($('.event-form').valid()) { 

     var loadUrl = '/auto-save-post.php'; 

     // ajax the results! 
     $.ajax({ 
      type: "POST", 
      data: $('.event-form').serialize(), 
      url: templateDir + loadUrl, // templateDir is declared in the footer 
      success: function(result) { 
       console.log('data sent!'); 
       console.log('sent to: ' + templateDir + loadUrl); 
      }, 
      error: function(jqXHR, textStatus, errorThrown) { 
       console.log(jqXHR + " :: " + textStatus + " :: " + errorThrown); 
      } 
     }); 


    } 

}); 


}); 
})(jQuery); 

PHP :

$data = $_POST['serialize']; 

    // set basic event info 
    $new_post = array(
     'post_type' => 'event', 
     'post_author' => $data['userID'], 
     'post_title' => $data['event-name'], 
     'post_date' => Date('Y-m-d H:i:s'), 
     'post_status' => 'draft', 
     'tax_input' => array( 
      'main-cat' => $data['event-main-cat'], 
      'sub-cat' => $data['sub-cat'] 
     ) 
    ); 

    // create the draft event 
    $post_id = wp_insert_post($new_post); 

수 아무도 내가 잘못 가고있는 것을 본다? 요청으로

편집 는 네트워크 탭의 정보 :

헤더 :

Remote Address: 168.249.133.103:80 
Request URL:http://website.co.uk/wp-content/themes/woa-2014/auto-save-post.php 
Request Method:POST 
Status Code:200 OK 
Request Headersview source 
Accept:*/* 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Cache-Control:no-cache 
Connection:keep-alive 
Content-Length:1616 
Content-Type:application/x-www-form-urlencoded; charset=UTF-8 
Cookie:wp-settings-time-3=1401887703; wp-settings-time-5=1402046083; wp-settings-1=libraryContent%3Dbrowse%26urlbutton%3Dfile; wp-settings-time-1=1402995301; G_USERSTATE_H4=113882842213089035920=1; PHPSESSID=4e2b3d299649e1410fe8501b6a25e0b1; linkedin_oauth_77r7rxe672z4s8_crc=null; wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_42509f9da25d0091a8295263478f1ff8=admin%7C1720385704%7Cd6b43843769a67a8e0746f7f784786d3; __atuvc=108%7C24%2C391%7C25%2C305%7C26%2C357%7C27%2C529%7C28 
Host:website.co.uk 
Origin:http://website.co.uk 
Pragma:no-cache 
Referer:http://website.co.uk/create-event/ 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36 
X-Requested-With:XMLHttpRequest 

Form Data 
userID:1 
event-name:test event name 
address-1:The Custard Factory 
postcode:B9 4AA 
address-2: 
city:Birmingham 
region:West Midlands 
country:UK 
lat:52.475514 
long:-1.8842237999999725 
phone:0121 285 5124 
phone-alt: 
email:[email protected] 
website: 
event-main-cat:2 
sub-cat: 
suitable-for-ages:18-30 

Response Headersview source 
Connection:Keep-Alive 
Content-Encoding:gzip 
Content-Length:183 
Content-Type:text/html 
Date:Wed, 09 Jul 2014 11:58:24 GMT 
Keep-Alive:timeout=5, max=1000 
Server:Apache 
Vary:Accept-Encoding,User-Agent 

응답

`Fatal error: Call to undefined function wp_insert_post() in /home/lukeseag/public_html/codeplayground/woa/wp-content/themes/woa-2014/auto-save-post.php on line 30` 
+0

whats'$ _POST [ 'serialize']'for? 그리고 나는 여기 어떤 종류의 인증도 보지 못한다 - 데이터를 게시하는 공개 API가 나쁜 생각 인 것 같습니다. – Steve

+0

ajax 연결의 Inspector 로그를 추가 할 수 있습니까? 이 작업을하려면 FF로 설치 한 후 Webkit의 Inspector (Chrome/Safari/다른 웹 키트 브라우저)를 설치하고 Firebug를 엽니 다. 그러면 네트워크 탭을 연 다음 요청을 보내면 모두/auto- save-post.php'을 열어 요청에 관한 모든 것을 코드 뷰로 복사하면 코드가 잘못되어있는 곳을 볼 수 있습니다. –

+0

'url ---> templateDir'은이 아이프 범위에서 사용할 수 없습니다 . – Jai

답변

2

이보십시오.

require_once $_SERVER["DOCUMENT_ROOT"]."/wp-load.php"; 
ini_set('display_errors', 1); 
error_reporting('E_ALL'); 

$user_id = get_current_user_id(); 

// Create post object 
$my_post = array(
    'post_title' => wp_strip_all_tags($_POST['post_title']), 
    'post_content' => $_POST['post_content'], 
    'post_status' => 'publish', 
    'post_author' => $user_id, 
    'tags_input' => implode(",","TAG1,TAG2"),//$_POST['tags'] 
); 

// Insert the post into the database 
$post_ID = wp_insert_post($my_post); 
echo $post_ID; 
+0

그냥 분명, 메인 내가'require_once $ _SERVER [ "DOCUMENT_ROOT"]. "/ wp-load.php";를 실행하지 않아서이 스크립트가 wordpress의 내장 함수를 이해하지 못했기 때문에 문제가 발생했습니다. – lukeseager

+0

예, WP 기능을 사용하려면이 줄로 시작해야합니다. 'requie_once $ _SERVER [ "DOCUMENT_ROOT"]. "/ wp-load.php"; – EliaszKubala

관련 문제