wpcf7_convert_to_cpt

The Contact Form 7 wpcf7 convert to cpt function.

Description

wpcf7_convert_to_cpt( $new_ver, $old_ver ); 

Parameters (2)

0. $new_ver
The new ver.
1. $old_ver
The old ver.

Usage

  1. if ( !function_exists( 'wpcf7_convert_to_cpt' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'contact-form-7/includes/upgrade.php'; 
  3.  
  4. // The new ver. 
  5. $new_ver = null; 
  6.  
  7. // The old ver. 
  8. $old_ver = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wpcf7_convert_to_cpt($new_ver, $old_ver); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/upgrade.php  
  1. function wpcf7_convert_to_cpt( $new_ver, $old_ver ) { 
  2. global $wpdb; 
  3.  
  4. if ( ! version_compare( $old_ver, '3.0-dev', '<' ) ) { 
  5. return; 
  6.  
  7. $old_rows = array(); 
  8.  
  9. $table_name = $wpdb->prefix . "contact_form_7"; 
  10.  
  11. if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) ) { 
  12. $old_rows = $wpdb->get_results( "SELECT * FROM $table_name" ); 
  13. } elseif ( ( $opt = get_option( wpcf7 ) ) && ! empty( $opt['contact_forms'] ) ) { 
  14. foreach ( (array) $opt['contact_forms'] as $key => $value ) { 
  15. $old_rows[] = (object) array_merge( $value, array( 'cf7_unit_id' => $key ) ); 
  16.  
  17. foreach ( (array) $old_rows as $row ) { 
  18. $q = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_old_cf7_unit_id'" 
  19. . $wpdb->prepare( " AND meta_value = %d", $row->cf7_unit_id ); 
  20.  
  21. if ( $wpdb->get_var( $q ) ) { 
  22. continue; 
  23.  
  24. $postarr = array( 
  25. 'post_type' => 'wpcf7_contact_form',  
  26. 'post_status' => 'publish',  
  27. 'post_title' => maybe_unserialize( $row->title ),  
  28. ); 
  29.  
  30. $post_id = wp_insert_post( $postarr ); 
  31.  
  32. if ( $post_id ) { 
  33. update_post_meta( $post_id, '_old_cf7_unit_id', $row->cf7_unit_id ); 
  34.  
  35. $metas = array( 'form', 'mail', 'mail_2', 'messages', 'additional_settings' ); 
  36.  
  37. foreach ( $metas as $meta ) { 
  38. update_post_meta( $post_id, '_' . $meta,  
  39. wpcf7_normalize_newline_deep( maybe_unserialize( $row->{$meta} ) ) );