$value ) if( $value ) $post_types[] = $post_type; // set up our WP_Query args to grab anything with legacy data $args = array( 'post_type' => isset( $post_types ) ? $post_types : array(), 'post_status' => 'any', 'posts_per_page' => -1, 'meta_key' => '_attachments', 'suppress_filters' => true, ); $query = new WP_Query( $args ); } $count = 0; // loop through each post if( $query ) { while( $query->have_posts() ) { // set up postdata $query->the_post(); // let's first decode our Attachments data $existing_attachments = get_post_meta( $query->post->ID, '_attachments', false ); $post_attachments = array(); // check to make sure we've got data if( is_array( $existing_attachments ) && count( $existing_attachments ) > 0 ) { // loop through each existing attachment foreach( $existing_attachments as $attachment ) { // decode and unserialize the data $data = unserialize( base64_decode( $attachment ) ); array_push( $post_attachments, array( 'id' => stripslashes( $data['id'] ), 'title' => stripslashes( $data['title'] ), 'caption' => stripslashes( $data['caption'] ), 'order' => stripslashes( $data['order'] ) )); } // sort attachments if( count( $post_attachments ) > 1 ) { usort( $post_attachments, 'attachments_cmp' ); } } // we have our Attachments entries // let's check to see if we're migrating after population has taken place $existing_attachments = get_post_meta( $query->post->ID, 'attachments', false ); if( !isset( $existing_attachments[0] ) ) $existing_attachments[0] = ''; $existing_attachments = json_decode( $existing_attachments[0] ); if( !is_object( $existing_attachments ) ) $existing_attachments = new stdClass(); // we'll loop through the legacy Attachments and save them in the new format foreach( $post_attachments as $legacy_attachment ) { // convert to the new format $converted_attachment = array( 'id' => $legacy_attachment['id'] ); // fields are technically optional so we'll add those separately // we're also going to encode them in the same way the main class does if( $title ) $converted_attachment['fields'][$title] = htmlentities( stripslashes( $legacy_attachment['title'] ), ENT_QUOTES, 'UTF-8' ); if( $caption ) $converted_attachment['fields'][$caption] = htmlentities( stripslashes( $legacy_attachment['caption'] ), ENT_QUOTES, 'UTF-8' ); // check to see if the existing Attachments have our target instance if( !isset( $existing_attachments->$instance ) ) { // the instance doesn't exist so we need to create it $existing_attachments->$instance = array(); } // we need to convert our array to an object $converted_attachment['fields'] = (object) $converted_attachment['fields']; $converted_attachment = (object) $converted_attachment; // append this legacy attachment to the existing instance array_push( $existing_attachments->$instance, $converted_attachment ); } // we're done! let's save everything in our new format $existing_attachments = version_compare( PHP_VERSION, '5.4.0', '>=' ) ? json_encode( $existing_attachments, JSON_UNESCAPED_UNICODE ) : json_encode( $existing_attachments ); // save it to the database update_post_meta( $query->post->ID, 'attachments', $existing_attachments ); // increment our counter $count++; } } return $count; } /** * Step 1 of the migration process. Allows the user to define the target instance and field names. * * @since 3.2 */ function prepare_migration() { if( !wp_verify_nonce( $_GET['nonce'], 'attachments-migrate-1') ) wp_die( __( 'Invalid request', 'attachments' ) ); ?>

Title field data will be migrated to this field name in Attachments 3.x. Leave empty to disregard.', 'attachments' ); ?>

Caption field data will be migrated to this field name in Attachments 3.x. Leave empty to disregard.', 'attachments' ); ?>

migrate( $_GET['attachments-instance'], $_GET['attachments-title'], $_GET['attachments-caption'] ); if( false == get_option( 'attachments_migrated' ) ) : ?>

: .