importexport.php (1247B)
1 <?php 2 $post_id = absint( $_GET['id'] ); 3 4 5 if ( ! empty( $_POST['sp_post_json'] ) ) { 6 // update 7 global $wpdb; 8 $json = json_decode( stripslashes( $_POST['sp_post_json'] ) ); 9 if ( json_last_error() !== JSON_ERROR_NONE ) { 10 wp_die( 'JSON is NOT valid' ); 11 } 12 $json = json_encode( $json ); 13 $tablename = $wpdb->prefix . 'posts'; 14 $r = $wpdb->update( 15 $tablename, 16 array( 17 'post_content_filtered' => $json, // string 18 ), 19 array( 'ID' => $post_id ), 20 array( 21 '%s', // value1 22 ), 23 array( '%d' ) 24 ); 25 if ( $r === false ) { 26 echo 'Update error' . PHP_EOL; 27 } else { 28 echo 'Updated' . PHP_EOL; 29 } 30 } 31 32 global $wpdb; 33 $tablename = $wpdb->prefix . 'posts'; 34 $sql = "SELECT * FROM $tablename"; 35 $sql .= ' WHERE ID = %s'; 36 $safe_sql = $wpdb->prepare( $sql, $post_id ); 37 $result = $wpdb->get_row( $safe_sql ); 38 39 40 $js = json_decode( $result->post_content_filtered ); 41 if ( json_last_error() === JSON_ERROR_NONE ) { 42 echo 'JSON is valid' . PHP_EOL; 43 } else { 44 echo 'JSON is NOT valid' . PHP_EOL; 45 } 46 47 48 ?> 49 <form method="post"> 50 <h1>Post JSON</h1> 51 <textarea name="sp_post_json" style="width:100%; height: 500px;"><?php echo esc_textarea($result->post_content_filtered); ?></textarea> 52 <input type="submit"> 53 </form>