Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 创建新帖子的AJAX表单(WordPress)_Php_Jquery_Ajax_Wordpress - Fatal编程技术网

Php 创建新帖子的AJAX表单(WordPress)

Php 创建新帖子的AJAX表单(WordPress),php,jquery,ajax,wordpress,Php,Jquery,Ajax,Wordpress,我试图通过AJAX将表单数据发送到一个php脚本,该脚本将在wordpress中创建一篇新文章。我的代码似乎应该可以工作,但由于某种原因,什么都没有发生 表单非常庞大,但我现在想了解的基本信息是: <form action="/create-event" method="POST" enctype="multipart/form-data" class="event-form"> <input type="hidden" name="userID" id="userID

我试图通过AJAX将表单数据发送到一个php脚本,该脚本将在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>
PHP:

有人能看出我哪里做错了吗

编辑 根据要求,网络选项卡中的信息:

标题:

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:info@b9media.co.uk
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`
试试这个

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;

什么是
$\u POST['serialize']
?我在这里也看不到任何类型的身份验证——发布数据的公共api似乎是个糟糕的主意。你能添加ajax连接的检查器日志吗?为此,请在FF安装和打开Firebug中打开WebKit的Inspector(Chrome/Safari/任何其他web kit浏览器),然后这两种方法都是一样的打开网络选项卡然后发送请求,您应该会看到调用
/auto save post.php
打开它并将请求的所有内容复制到代码视图中,这样可以帮助我们看到您的代码哪里出错了我想您的
url-->templateDir
在此iife范围内不可用。您好@Martin Barker,刚刚检查了这个,在响应中我得到了
致命错误:调用未定义函数wp_insert_post()在第27行的/home/lukesag/public_html/codeplayde/woa/wp content/themes/woa-2014/auto-save-post.php中
我猜这就是问题所在?是的,很抱歉我没有给你回复,但你得到了答案,很高兴我能帮助你得到答案,尽管我向你展示了如何在发出XHR请求时从服务器端获取调试/错误很明显,主要的问题是因为我没有做
require\u once$\u SERVER[“DOCUMENT\u ROOT”]。“/wp load.php”
,这意味着此脚本不理解wordpress的内置函数。是的,当您要使用WP函数时,始终必须从以下行开始
require\u once$\u SERVER[“DOCUMENT\u ROOT”]。“/WP load.php”
`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`
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;