Save contact form 7 data to database with image attachment upload and reCaptcha checking



function create_attachment($source_filepath)
{    
   $new_image_name = time().'_'.basename($source_filepath);
   $filetype = wp_check_filetype($new_image_name, null);    
   $wp_upload_dir = wp_upload_dir();
   $attach_fileName = $wp_upload_dir['path'] . '/' . $new_image_name;	
   copy($source_filepath, $attach_fileName);
   // Prepare an array of post data for the attachment.
   $attachment = array(
     'guid'           => $attach_fileName,
     'post_mime_type' => $filetype['type'],
     'post_title'     => preg_replace('/\.[^.]+$/', '', $new_image_name),
     'post_content'   => '',
     'post_status'    => 'inherit'
   );
   $attach_id = wp_insert_attachment($attachment, $attach_fileName);

   require_once(ABSPATH . 'wp-admin/includes/image.php');
   // Generate the metadata for the attachment, and update the database record.
   $attach_data = wp_generate_attachment_metadata($attach_id, $attach_fileName);
   wp_update_attachment_metadata($attach_id, $attach_data);
   return $attach_data;
}

function save_contactform7_before_send_mail( $form_to_DB ) { 		
   $form_to_DB = WPCF7_Submission::get_instance();
   if ( $form_to_DB ) {
        $formData = $form_to_DB->get_posted_data();
        $leadtype = $formData['lead-type'];
	$recaptcha_response = $form_to_DB->recaptcha['response']['success'];
	if($recaptcha_response) {
   	  $tableName = '';
	  $params = [];
	  global $wpdb;
	  if($leadtype == 'sell-modal'){
	    $uploaded_files = $form_to_DB->uploaded_files();
	    $imgdata = [];
	    if ($uploaded_files) {
	      foreach ($uploaded_files as $fieldName => $filepath) {
	        $imgdata = create_attachment($filepath[0]);
	      }
	    }
	    $your_name = isset($formData['your-name']) ? sanitize_text_field(trim($formData['your-name'])) : "";
	    $your_email = isset($formData['your-email']) ? sanitize_text_field(trim($formData['your-email'])) : "";
	    $your_phone = isset($formData['your-phone']) ? sanitize_text_field(trim($formData['your-phone'])) : "";				
            $your_address = isset($formData['your-address']) ? sanitize_text_field(trim($formData['your-address'])) : "";
	    $tableName = $wpdb->prefix.'sells';
	    $params = [
	      'full_name' => $your_name,
	      'email' => $your_email,
	      'phone' => $your_phone,
	      'address' => $your_address,
	      'image' => json_encode($imgdata)				
	    ];
	 }			
	 if(!empty($params) && $tableName != ''){
	    $wpdb->insert($tableName, $params);
	 }
      }		
   }
}
remove_all_filters ('wpcf7_before_send_mail');
add_action( 'wpcf7_before_send_mail', 'save_contactform7_before_send_mail' );

Adding contact form 7 to Elementor Custom Widget | WordPress



<?php
/**
 * Elementor  Widget.
 *
 * Elementor widget that inserts an embbedable content into the page, from any given URL.
 *
 * @since 1.0.0
 */

if (!defined('ABSPATH'))
    die('No script kiding please');
    
class Elementor_Contact_Widget extends \Elementor\Widget_Base {

/**
* Get widget name.
*
* Retrieve  widget name.
*
* @since 1.0.0
* @access public
*
* @return string Widget name.
*/
public function get_name() {
    return 'my-contact';
}

/**
* Get widget title.
*
* Retrieve  widget title.
*
* @since 1.0.0
* @access public
*
* @return string Widget title.
*/
public function get_title() {
    return __( 'My Contact', 'plugin-name' );
}

/**
* Get widget icon.
*
* Retrieve  widget icon.
*
* @since 1.0.0
* @access public
*
* @return string Widget icon.
*/
public function get_icon() {
    return 'fa fa-code';
}

/**
* Get widget categories.
*
* Retrieve the list of categories the  widget belongs to.
*
* @since 1.0.0
* @access public
*
* @return array Widget categories.
*/
public function get_categories() {
    return [ 'basic' ];
}

/**
* Get all entry of contact form 7
* @return array
*/
public function get_contact_form_7_content(){
    $posts = get_posts(array(
        'post_type'     => 'wpcf7_contact_form'
    ));
    $contact7 = [];
    $contact7[0] = 'Please select a contact form';
    foreach($posts as $k => $post){
        $contact7[$post->ID] = __( $post->post_title, 'plugin-name' );
    }
    return $contact7;
}

/**
* Register  widget controls.
*
* Adds different input fields to allow the user to change and customize the widget settings.
*
* @since 1.0.0
* @access protected
*/
protected function _register_controls() {

    $this->start_controls_section(
        'content_section',
        [
            'label' => __( 'Content', 'plugin-name' ),
            'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
        ]
    );

    $this->add_control(
        'contact_form',
        [
            'label' => __( 'Contact Form', 'plugin-name' ),
            'type' => \Elementor\Controls_Manager::SELECT,
            'default' => 0,
            'options' => $this->get_contact_form_7_content()
        ]
    );		

    $this->end_controls_section();
    

}

/**
* Render  widget output on the frontend.
*
* Written in PHP and used to generate the final HTML.
*
* @since 1.0.0
* @access protected
*/
protected function render() { ?>
    <?php $settings = $this->get_settings_for_display(); ?>

    <div class="section-gap contact-request-area">
        <div class="container">
            <div class="section-heading">
                <h3 class="title">We would love to hear from you.</h3>
            </div>
            <div class="contact-box-wrap">
                <?php echo ($settings['contact_form'] > 0) ? do_shortcode('[contact-form-7 id="'. $settings['contact_form'].'"]') : '';?>
            </div>
        </div>
    </div>

<?php }

}