• Hello, I’m trying to create a function to insert submit_time into another database table. I’m trying to call submit_time inside a function in order to pass the value.

    I tried taking the timestamp value of private function submit() (in /plugins/contact-form-7/includes/submission.php), however this timestamp is not equivalent to the submit_time used in cfdb.

    Hence how can I get call the submit_time in a function?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Michael Simpson

    (@msimpson)

    Are you trying to do this when a submission comes in or afterward?

    Thread Starter antdigital

    (@antdigital)

    I’m trying to call it when a submission comes in. Calling the function inside elseif ( $this->mail() ) { within the submit() .

    Plugin Author Michael Simpson

    (@msimpson)

    See: http://cfdbplugin.com/?page_id=747

    Something like this:

    
    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php');
    
    function insert_submit_time_into_other_db($formData) {
        if (!isset($posted_data['submit_time'])) {
            $plugin = new CF7DBPlugin();
            $posted_data['submit_time'] = $plugin->generateSubmitTime();
        }
        
        // TODO: Save $posted_data['submit_time'] into your other DB
    
        return $formData; // be sure to return it
    }
    
    add_filter('cfdb_form_data', 'insert_submit_time_into_other_db');
    
    Thread Starter antdigital

    (@antdigital)

    Hi Michael,

    Thank you so much for the detailed guide, however the value still does not tally.
    (eg. 1476261782.1369 in my function, but the actual submit_time is 1476261781.9349)

    Could the problem be in where I call the function.

    
    ///wp-content/plugins/contact-form-7/includes/submission.php
    
    private function submit() {
    		if ( ! $this->is( 'init' ) ) {
    			return $this->status;
    		}
    
    		$this->meta = array(
    			'remote_ip' => isset( $_SERVER['REMOTE_ADDR'] )
    				? preg_replace( '/[^0-9a-f.:, ]/', '', $_SERVER['REMOTE_ADDR'] )
    				: '',
    			'user_agent' => isset( $_SERVER['HTTP_USER_AGENT'] )
    				? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '',
    			'url' => preg_replace( '%(?<!:|/)/.*$%', '',
    				untrailingslashit( home_url() ) ) . wpcf7_get_request_uri(),
    			'timestamp' => current_time( 'timestamp' ),
    			'unit_tag' => isset( $_POST['_wpcf7_unit_tag'] )
    				? $_POST['_wpcf7_unit_tag'] : '' );
    
    		$contact_form = $this->contact_form;
    
    		if ( ! $this->validate() ) { // Validation error occured
    			$this->status = 'validation_failed';
    			$this->response = $contact_form->message( 'validation_error' );
    
    		} elseif ( ! $this->accepted() ) { // Not accepted terms
    			$this->status = 'acceptance_missing';
    			$this->response = $contact_form->message( 'accept_terms' );
    
    		} elseif ( $this->spam() ) { // Spam!
    			$this->status = 'spam';
    			$this->response = $contact_form->message( 'spam' );
    
    		} elseif ( $this->mail() ) {
    insert_submit_time_into_other_db($formData); //========!FUNCTION HERE!========
    			$this->status = 'mail_sent';
    			$this->response = $contact_form->message( 'mail_sent_ok' );
    		
    			do_action( 'wpcf7_mail_sent', $contact_form );
    
    		} else {
    			$this->status = 'mail_failed';
    			$this->response = $contact_form->message( 'mail_sent_ng' );
    
    			do_action( 'wpcf7_mail_failed', $contact_form );
    		}
    
    		$this->remove_uploaded_files();
    
    		return $this->status;
    	}
    • This reply was modified 9 years, 7 months ago by antdigital.
    Plugin Author Michael Simpson

    (@msimpson)

    It looks like you are modifying CF7 code which is a bad idea. I provided you a solution using a hook. The link in my previous post describes where to put it.

    Thread Starter antdigital

    (@antdigital)

    Hi Michael,
    I took your advice to move my function, from my “/theme/functions.php” to <Add Shortcodes Actions And Filters> plugin.

    However I’ve not change the place I called the function, as on_sent_ok:”insert_submit_time_into_other_db($formData);” gave me an referenceerror (insert_submit_time_into_other_db is not defined).

    I see the “lagged time” problem is caused by

        if (!isset($posted_data['submit_time'])) {
            $plugin = new CF7DBPlugin();
            $posted_data['submit_time'] = $plugin->generateSubmitTime();
        }

    submit_time was not set and was generated again.

    What could be the reason that $posted_data[‘submit_time’] is unable to get the submit_time in my function (on_sent_ok:”insert_submit_time_into_other_db($formData);)

    Sorry for the troubles but thank you!

    Thread Starter antdigital

    (@antdigital)

    I think I should be clearer.

    So I followed your guidance and this is my function codes

    function testform($formData) {
    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php');
        // Change $formData
    	    if (!isset($posted_data['submit_time'])) {
            $plugin = new CF7DBPlugin();
            $posted_data['submit_time'] = $plugin->generateSubmitTime();
        }
    	 $formData->posted_data['email'] = $posted_data['submit_time'];
    
        return $formData; // be sure to return it
    }
     
    add_filter('cfdb_form_data', 'testform');

    and gotten this result (https://img42.com/j0pXe)
    However as shown in the screenshot, I actually wanted the submit_time and field_value to have the same value (1476347176.3544 on both side instead of 1476347176.4563 on the field_value side).

    Plugin Author Michael Simpson

    (@msimpson)

    Sorry, change
    if (!isset($posted_data[‘submit_time’]))
    To
    if (!isset($formData->posted_data[‘submit_time’]))

    Change
    $posted_data[‘submit_time’] = $plugin->generateSubmitTime();
    To
    $formData->posted_data[‘submit_time’] = $plugin->generateSubmitTime();

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘How to call submit_time’ is closed to new replies.