wpcf7_convert_to_cpt
The Contact Form 7 wpcf7 convert to cpt function.
Description
Parameters (2)
- 0. $new_ver
- The new ver.
- 1. $old_ver
- The old ver.
Usage
if ( !function_exists( 'wpcf7_convert_to_cpt' ) ) { require_once ABSPATH . PLUGINDIR . 'contact-form-7/includes/upgrade.php'; } // The new ver. $new_ver = null; // The old ver. $old_ver = null; // NOTICE! Understand what this does before running. $result = wpcf7_convert_to_cpt($new_ver, $old_ver);
Defined (1)
The function is defined in the following location(s).
- /includes/upgrade.php
- function wpcf7_convert_to_cpt( $new_ver, $old_ver ) {
- global $wpdb;
- if ( ! version_compare( $old_ver, '3.0-dev', '<' ) ) {
- return;
- }
- $old_rows = array();
- $table_name = $wpdb->prefix . "contact_form_7";
- if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) ) {
- $old_rows = $wpdb->get_results( "SELECT * FROM $table_name" );
- } elseif ( ( $opt = get_option( wpcf7 ) ) && ! empty( $opt['contact_forms'] ) ) {
- foreach ( (array) $opt['contact_forms'] as $key => $value ) {
- $old_rows[] = (object) array_merge( $value, array( 'cf7_unit_id' => $key ) );
- }
- }
- foreach ( (array) $old_rows as $row ) {
- $q = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_old_cf7_unit_id'"
- . $wpdb->prepare( " AND meta_value = %d", $row->cf7_unit_id );
- if ( $wpdb->get_var( $q ) ) {
- continue;
- }
- $postarr = array(
- 'post_type' => 'wpcf7_contact_form',
- 'post_status' => 'publish',
- 'post_title' => maybe_unserialize( $row->title ),
- );
- $post_id = wp_insert_post( $postarr );
- if ( $post_id ) {
- update_post_meta( $post_id, '_old_cf7_unit_id', $row->cf7_unit_id );
- $metas = array( 'form', 'mail', 'mail_2', 'messages', 'additional_settings' );
- foreach ( $metas as $meta ) {
- update_post_meta( $post_id, '_' . $meta,
- wpcf7_normalize_newline_deep( maybe_unserialize( $row->{$meta} ) ) );
- }
- }
- }
- }