Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating a front end posting form using wp_insert_post that asks a user for three basic fields: their first and last name and their email address. If the user is logged in I pre-populate the text fields and ignore them when submitting a form.

If however the user is not logged in, my function checks to see if the email entered is registered. If it is, the function grabs the associated user_id and assigns that user as the post_author. If the email is not associated, a new user is created via wp_insert_user.

The form is returning this error:

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

What am I missing?

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');

The form follows the VoodooPress model.

Edit: Would using the WP_User class make a difference? That's a little over my programming head but I could find someone to do it if that would be a better fix.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

The header problem is actually this problem:

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

You are getting those errors because somewhere you are sending an object when you shouldn't be. When that error prints it sends content to the browser before the headers are sent, so when the headers actually are sent you get an error.

I am not spotting where you are sending an object though, sadly. var_dump all of your variables and you should find the problem.

For example.

$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);
share|improve this answer
    
Thanks, that helped find the answer. Which was caused by my form missing a name="" for the email. I really appreciate it. For reference for others I used print '<pre>'; var_dump( $new_post); print '</pre>';. –  mattrepublic Jan 9 '13 at 20:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.