• I’m trying to process a form and the page should forward to header('Location: https://www.test.co.uk/thankyou');. I’ve turned on WP_DEBUG and the page is still blank. I’ve read that it may be a fatal error. This is my full page.

    <?php
    $art_title = $_POST['art_title'];
    
    $i = 0;
    foreach($art_title as $value) {
    $art_title = $_POST['art_title'][$i];
    $art_width = $_POST['art_width'][$i];
    $art_height = $_POST['art_height'][$i];
    $art_price = $_POST['art_price'][$i];
    $art_creation_date = $_POST['art_creation_date'][$i];
    $art_upload = $_FILES['art_upload'][$i];
    
    	 $post = array(
          'post_content'   => '', // The full text of the post.
          'post_name'      => $art_title, // The name (slug) for your post
          'post_title'     => $art_title, // The title of your post.
          'post_status'    => 'draft', // Default 'draft'.
          'post_type'      => 'product', // Default 'post'.
          'post_author'    => '', // The user ID number of the author. Default is the current user ID.
          'menu_order'     => '0', // If new post is a page, sets the order in which it should appear in supported menus. Default 0.
          'post_password'  => '', // Password for post, if any. Default empty string.
          'post_excerpt'   => '', // For all your post excerpt needs.
          'comment_status' => 'open' , // Default is the option 'default_comment_status', or 'closed'.
          'post_category'  => '' // Default empty.
        );
        $post_id = wp_insert_post( $post);
        wp_set_post_categories( $post_id);
        add_post_meta($post_id, '_width', $art_width, true);
    	add_post_meta($post_id, '_height', $art_height, true);
    
        $filename = $_FILES["art_upload"][[$i]["name"];
        $uploads = wp_upload_dir();
    
        //file_put_contents($file, $image_data);
        $file_array = array(
                'name'          => $_FILES['art_upload'][$i]['name'],
                'type'          => $_FILES['art_upload'][$i]['type'],
                'tmp_name'      => $_FILES['art_upload'][$i]['tmp_name'],
                'error'         => $_FILES['art_upload'][$i]['error'],
                'size'          => $_FILES['art_upload'][$i]['size']
        );
    
        // required for wp_handle_upload() to upload the file
        $upload_overrides = array( 'test_form' => FALSE );
    
        if ( !empty( $file_array['name'] ) ) {
           // upload the file to the server
            $uploaded_file = wp_handle_upload( $file_array, $upload_overrides );
    
                // checks the file type and stores in in a variable
            $wp_filetype = wp_check_filetype( basename( $uploaded_file['art_upload'] ), null );
               // set up the array of arguments for "wp_insert_post();"
            $attachment = array(
    
                'post_mime_type' => $wp_filetype['type'],
                'post_title' => preg_replace('/.[^.]+$/', '', basename( $uploaded_file['art_upload'] ) ),
                'post_content' => '',
                'post_author' => '',
                'post_status' => 'inherit',
                'post_type' => 'attachment',
                'guid' => $uploads['url'] . '/' . $file_array['name']
            );
    
            // insert the attachment post type and get the ID
            $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
            $attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_file['art_upload'] );
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            // update the attachment metadata
            wp_update_attachment_metadata( $attach_id,  $attach_data );
            $post_thumbnail_id = get_post_thumbnail_id( $post_id );
            set_post_thumbnail( $post_id, $post_thumbnail_id );
        }
    
    $i++;
    }
    
    header('Location: https://www.test.co.uk/thankyou');
    ?>

    What do i need to add to this code to get it working? require code?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘wp_insert_post and blank page’ is closed to new replies.