如何在没有`Headers已发送`错误的情况下同时使用`wp_INSERT_USER`和`wp_INSERT_POST`?

时间:2013-01-09 作者:m-torin

我正在使用创建前端过帐表单wp_insert_post 这要求用户输入三个基本字段:他们的名字、姓氏和电子邮件地址。如果用户已登录,我会预先填充文本字段,并在提交表单时忽略它们。

但是,如果用户未登录,“我的功能”会检查if the email entered is registered. 如果是,函数将获取关联的user\\u id并将该用户指定为post\\u作者。如果电子邮件未关联,则通过以下方式创建新用户wp_insert_user.

表单返回此错误:

Warning: mysql_real_escape_string() expects parameter 1 to be string, object given in /home/wp-includes/wp-db.php on line 885

Warning: mysql_real_escape_string() expects parameter 1 to be string, object given in /home/wp-includes/wp-db.php on line 885

Warning: Cannot modify header information - headers already sent by (output started at /home/wp-includes/wp-db.php:885) in /home/wp-includes/pluggable.php on line 876
我错过了什么?

if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "new_post") {

    // $tags = $_POST[\'post_tags\'];
    $first_name = $_POST[\'first_name\'];
    $last_name = $_POST[\'last_name\'];
    $author_email = $_POST[\'author_email\'];

    $business_name = $_POST[\'business_name\'];
    $business_city = $_POST[\'business_city\'];
    $business_state = $_POST[\'business_state\'];
    $methodgroup = $_POST[\'methodgroup\'];

    if ( ! is_user_logged_in() ) {
        if ( email_exists( $author_email ) ) {
            // email has an account
            require_once(ABSPATH . WPINC . \'/ms-functions.php\');
            $userid = get_user_id_from_string( $author_email );
        }
        else {
            // create an account
            $user_id = wp_insert_user( 
                array (
                    \'first_name\' => $first_name,
                    \'last_name\' => $last_name,
                    \'user_email\' => $author_email,
                    \'user_login\' => $author_email,
                )
            ) ;
            wp_set_password($last_name, $user_id);
        }
    }
    else {
        $user_id = get_current_user_id();
    }

    // ADD THE FORM INPUT TO $new_post ARRAY
    $new_post = array(
        \'post_title\'    =>  \'Project\',
        \'post_status\'   =>  \'draft\',
        \'post_type\'     =>  \'project\',
        \'post_author\'   =>  $user_id,
        \'business_name\' =>  $business_name,
        \'business_city\' =>  $business_city,
        \'business_state\' => $business_state,
        \'methodgroup\'   =>  $methodgroup,

    //  Other options   
    //  \'ID\'             => [ <post id> ] //Are you updating an existing post?
    //  \'menu_order\'     => [ <order> ] //If new post is a page, it sets the order in which it should appear in the tabs.
    //  \'comment_status\' => [ \'closed\' | \'open\' ] // \'closed\' means no comments.
    //  \'ping_status\'    => [ \'closed\' | \'open\' ] // \'closed\' means pingbacks or trackbacks turned off
    //  \'pinged\'         => [ ? ] //?
    //  \'post_author\'    => [ <user ID> ] //The user ID number of the author.
    //  \'post_content\'   => $description,
    //  \'post_date\'      => [ Y-m-d H:i:s ] //The time post was made.
    //  \'post_date_gmt\'  => [ Y-m-d H:i:s ] //The time post was made, in GMT.
    //  \'post_excerpt\'   => [ <an excerpt> ] //For all your post excerpt needs.
    //  \'post_name\'      => [ <the name> ] // The name (slug) for your post
    //  \'post_parent\'    => [ <post ID> ] //Sets the parent of the new post.
    //  \'post_password\'  => [ ? ] //password for post?
    //  \'post_status\'    => [ \'draft\' | \'publish\' | \'pending\'| \'future\' | \'private\' | custom registered status ]
    //  \'post_type\'      => [ \'post\' | \'page\' | \'link\' | \'nav_menu_item\' | custom post type ]
    //  \'tags_input\'     => array($tags),
    //  \'tax_input\'      => [ array( \'taxonomy_name\' => array( \'term\', \'term2\', \'term3\' ) ) ] // support for custom taxonomies. 
    );

    //SAVE THE POST
    $pid = wp_insert_post($new_post, true);

    //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
    // wp_set_post_tags($pid, $_POST[\'post_tags\']);

    //REDIRECT TO THE NEW POST ON SAVE
    $link = get_permalink( $pid );
    wp_redirect( $link );

    //ADD OUR CUSTOM FIELDS 
    add_post_meta($pid, \'_trade_name\', $business_name, true); 
    add_post_meta($pid, \'_location_1_city\', $business_city, true); 
    add_post_meta($pid, \'_location_1_state\', $business_state, true); 
    add_post_meta($pid, \'_method\', $methodgroup, true); 

    /*
    //INSERT OUR MEDIA ATTACHMENTS
    if ($_FILES) {
        foreach ($_FILES as $file => $array) {
        $newupload = insert_attachment($file,$pid);
        // $newupload returns the attachment id of the file that
        // was just uploaded. Do whatever you want with that now.
        }
    } // END THE IF STATEMENT FOR FILES
    */

} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST YO
do_action(\'wp_insert_post\', \'wp_insert_post\');
表格如下所示VoodooPress 模型

编辑:将使用WP_User class 有影响这有点超出了我的编程能力,但如果这是一个更好的解决方案,我可以找人来做。

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

标题问题实际上是这个问题:

警告:mysql\\u real\\u escape\\u string()要求参数1为字符串,在/home/wp中给出的对象包括/wp db。php在线885

警告:mysql\\u real\\u escape\\u string()要求参数1为字符串,在/home/wp中给出的对象包括/wp db。php在线885

您之所以会收到这些错误,是因为您在某个地方发送了一个不应该发送的对象。当打印该错误时,它会在发送标题之前将内容发送到浏览器,因此当标题实际发送时,您会收到一个错误。

遗憾的是,我没有发现你在哪里发送对象。var_dump 所有的变量,你应该找到问题所在。

例如

$new_post = array(
    \'post_title\'    =>  \'Project\',
    \'post_status\'   =>  \'draft\',
    \'post_type\'     =>  \'project\',
    \'post_author\'   =>  $user_id,
    \'business_name\' =>  $business_name,
    \'business_city\' =>  $business_city,
    \'business_state\' => $business_state,
    \'methodgroup\'   =>  $methodgroup );

var_dump($new_post);

结束