AJAX表单以创建新帖子(WordPress) - php

我正在尝试通过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>

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`

参考方案

尝试这个。

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;

WordPress:在网站上仅加载一个jquery脚本 - php

刚要启动一个WordPress网站,但注意到它当前正在加载两个jquery文件,一个包含在wp-includes中,另一个来自我的header.php,是否有一种方法可以使wordpress在前端加载wp-includes?做了大量的搜索,并且对此的唯一提及似乎包括以下代码,但是我找不到关于它的任何文档,任何想法?<?php wp_enqueue_sc…

如何从php中获取datatables jQuery插件的json数据 - php

我是PHP的新手,正在尝试使用Datatables jQuery插件。我知道我必须从.php文件中获取数据,然后在数据表中使用它,但是我似乎无法通过ajax将其传递给数据表。我设法从数据库中获取数据,并对其进行json编码,但是后来我不知道如何在我的index.php文件中调用它,并在其中显示它。我究竟做错了什么?这是我的代码:HTML(已编辑): <…

PHP JQuery复选框 - php

我有以下片段。 var myData = { video: $("input[name='video[]']:checked").serialize(), sinopse: $("#sinopse").val(), dia: $("#dia").val(), quem: $(&#…

从列表中排除类别-Wordpress - php

我正在尝试从以下列表中排除类别:<?php $categories = get_categories( 'orderby=id&exclude=1,'. getOption('promo-categorie', false) ); foreach( $categories as $category ) :…

在发布选项中使用Wordpress颜色选择器 - php

我创建发布选项,并希望在其中实现wordpress颜色选择器核心我尝试了从许多教程和源代码中获得的这段代码,但不幸的是,它根本无法正常工作,就像我从未添加代码一样。的HTML<input name="mv_cr_section_color" type="text" id="mv_cr_section_c…