admin管理员组

文章数量:1123796

I'm creating a custom WordPress post type to hold the contents of a CSV file so that it can be processed later.

The post is created correctly, with all the right information with the exception of the actual contents, which are blank.

I have verified that $csv_data does contain the full contents of the file, and I have also verified that if I override this value and save something such as 'this is a test' to the post_content, then it works.

Am I overlooking something horribly basic here? (and this includes why on earth would you do that in the first place!)

Here's the code

function handle_csv_upload() {
//*****
//***** Manage the actual upload of the CSV file
    if (isset($_POST['upload_csv'])) {
        
        if (!empty($_FILES['csv_file']['tmp_name'])) {
            $csv_file = $_FILES['csv_file']['tmp_name'];
            $csv_data = file_get_contents($csv_file);
                        
            // Create the new CSV file post
            $post_id = wp_insert_post([
                'post_title' => sanitize_file_name($_FILES['csv_file']['name']),
                'post_type' => 'user_reg_files',
                'post_status' => 'publish',
                ], 
                true );
            if ( is_wp_error( $post_id ) ) {
                // Handle error
                echo '<p>Error creating post: ' . $post_id->get_error_message() . "</p>";
            } else {
                // Post updated successfully
                echo '<p>Post created! New post ID: ' . $post_id . "</p>";
            }           
            
            // Save CSV data as post content
            if ($post_id) {
                echo "<p>Post ID = " . $post_id . "</p>" ;
                echo "<p>" . $csv_data . "</p>" ;            
                wp_update_post([
                    'ID' => $post_id,
                    'post_content' => $csv_data,
                    ], 
                    true );
            }

            if ( is_wp_error( $post_id ) ) {
                // Handle error
                echo '<p>Error updating post: ' . $post_id->get_error_message() . "</p>";

            } else {
                // Post updated successfully
                echo '<p>Post updated successfully</p>';
            }           
            //echo '<script>alert("CSV file uploaded successfully. Post id is ". $post_id)</script>'; 
            header('Location: ' . admin_url('admin.php?page=user-registrations-uploader') );
            
        } else {
            echo 'Please select a CSV file to upload.';
        } 
    }
}

I'm creating a custom WordPress post type to hold the contents of a CSV file so that it can be processed later.

The post is created correctly, with all the right information with the exception of the actual contents, which are blank.

I have verified that $csv_data does contain the full contents of the file, and I have also verified that if I override this value and save something such as 'this is a test' to the post_content, then it works.

Am I overlooking something horribly basic here? (and this includes why on earth would you do that in the first place!)

Here's the code

function handle_csv_upload() {
//*****
//***** Manage the actual upload of the CSV file
    if (isset($_POST['upload_csv'])) {
        
        if (!empty($_FILES['csv_file']['tmp_name'])) {
            $csv_file = $_FILES['csv_file']['tmp_name'];
            $csv_data = file_get_contents($csv_file);
                        
            // Create the new CSV file post
            $post_id = wp_insert_post([
                'post_title' => sanitize_file_name($_FILES['csv_file']['name']),
                'post_type' => 'user_reg_files',
                'post_status' => 'publish',
                ], 
                true );
            if ( is_wp_error( $post_id ) ) {
                // Handle error
                echo '<p>Error creating post: ' . $post_id->get_error_message() . "</p>";
            } else {
                // Post updated successfully
                echo '<p>Post created! New post ID: ' . $post_id . "</p>";
            }           
            
            // Save CSV data as post content
            if ($post_id) {
                echo "<p>Post ID = " . $post_id . "</p>" ;
                echo "<p>" . $csv_data . "</p>" ;            
                wp_update_post([
                    'ID' => $post_id,
                    'post_content' => $csv_data,
                    ], 
                    true );
            }

            if ( is_wp_error( $post_id ) ) {
                // Handle error
                echo '<p>Error updating post: ' . $post_id->get_error_message() . "</p>";

            } else {
                // Post updated successfully
                echo '<p>Post updated successfully</p>';
            }           
            //echo '<script>alert("CSV file uploaded successfully. Post id is ". $post_id)</script>'; 
            header('Location: ' . admin_url('admin.php?page=user-registrations-uploader') );
            
        } else {
            echo 'Please select a CSV file to upload.';
        } 
    }
}

Share Improve this question edited Mar 27, 2024 at 15:59 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Mar 27, 2024 at 10:17 JMouseJMouse 111 bronze badge 1
  • 1 Are you getting an error? And can you add var_dump( $csv_data ) and make sure it's structured as data that can be saved as post_content. – Tony Djukic Commented Mar 27, 2024 at 13:57
Add a comment  | 

1 Answer 1

Reset to default 0

If your tests are correct the problem is with the data in $csv_data. Before saving something to the database WP runs a lot of filters. Some are standard, others may be added by plugins. So, most likely some piece of code thinks the content of $csv_data is not fit to be stored in the database.

You may try to track this down. Or you could bypass the filters. Only do this when you are very sure about what is in $csv_data.

Right before a post is actually updated (so, after the data has passed all sanitation filters) wp_insert_post offers a final possibility to filter the data in this line:

$data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr, $update );

So, you can build a filter to replace the sanitized content with the one that was originally passed to the function, which is stored in $unsanitized_postarr:

function wpse424390_restore_content ($data, $postarr, $unsanitized_postarr, $update) {
  $data['post_content'] = $unsanitized_postarr['post_content'];
  return $data;
}

Now, you don't want this filter to be present all the time, because then it would trigger at every save action. You would use add_filter('wp_insert_post_data','wpse424390_restore_content',10,2) right before your call to wp_update_post and remove_filter('wp_insert_post_data','wpse424390_restore_content') directly after.

本文标签: custom post typesCannot save the contents of a csv file as postcontent within wpupdatepost