• Resolved mainliborg

    (@mainliborg)


    I’m trying to figure out if there is a way to have a contact form 7 field (date) be mapped to the publish date for the new post. I’d like the user to be able to set the date for when the new post goes live when they submit the form. The standard list of default post fields doesn’t have this as an option. Is there a way to do this?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Aurovrata Venet

    (@aurovrata)

    sure, you can achieve this programmatically once the submission has been mapped using the action hook provided (see screenshot #8, hook #16).

    mapped to the publish date for the new post

    see this blog post to update the date.

    I’d like the user to be able to set the date for when the new post goes live when they submit the form.

    however, if you want to automatically publish the post in the future, you also need to update the post status.

    Use the wp_update_post() on the newly create post to set the publication to a future date, you need to update 3 parameters,

    • post_status to future
    • post_date
    • post_date_gmt

    The post_date_gmt is used by the cron service to actually publish your post automatically.

    Thread Starter mainliborg

    (@mainliborg)

    add_action('cf7_2_post_form_submitted_to_post', 'new_post_mapped',10,4);
    	/**
    	* Function to take further action once form has been submitted and saved as a post.  Note this action is only fired for submission which has been submitted as opposed to saved as drafts.
    	* @param string $post_id new post ID to which submission was saved.
    	* @param array $cf7_form_data complete set of data submitted in the form as an array of field-name=>value pairs.
    	* @param string $cf7form_key unique key to identify your form.
    	* @param array $submitted_files array of files submitted in the form, if any file fields are present.
    	*/
    	function new_post_mapped($post_id, $cf7_form_data, $cf7form_key, $submitted_files){
    		wp_update_post(
        array (
            'ID' => $post_id,
            'post_date' => $cf7_form_data["start-date-closing"],
    		
            'post_date_gmt' => get_gmt_from_date( $cf7_form_data["start-date-closing"] )
        )
    );
    	}

    Ok, So I added the above to my functions and it works to schedule the post for a future date. BUT, when this code is active the meta post mappings don’t work anymore. I need to write 2 custom fields and they work if the above is disabled, but stop working when it’s enabled. I also have the

    add_filter( 'cf7_2_post_status_post', 'publish_new_post',10,3);
    	/**
    	* Function to change the post status of saved/submitted posts.
    	* @param string $status the post status, default is 'draft'.
    	* @param string $ckf7_key unique key to identify your form.
    	* @param array $submitted_data complete set of data submitted in the form as an array of field-name=>value pairs.
    	* @return string a valid post status ('publish'|'draft'|'pending'|'trash')
    	*/
    	function publish_new_post($status, $ckf7_key, $submitted_data){
    	/*The default behaviour is to save post to 'draft' status.  If you wish to change this, you can use this filter and return a valid post status: 'publish'|'draft'|'pending'|'trash'*/
    	return 'publish';
    	}

    code set as well in functions.

    So, is there an error in my code? or is there a bug/issue in the plugin when these are both running?

    Plugin Author Aurovrata Venet

    (@aurovrata)

    it shouldn’t affect the mapping as the action hook is fired at the end of the mapping process. Let me check and get back to you.

    Plugin Author Aurovrata Venet

    (@aurovrata)

    Are you running your site in WP_DEBUG mode to catch any errors on your site?

    you’re trying to publish your post return 'publish'; as well as set a future publication, which likely results in an error

    Plugin Author Aurovrata Venet

    (@aurovrata)

    Just tested on my local server and everything works as expected. I reckon you have some errors in your process flow. Setup WP_DEBUG mode to log all errors and check the debug.log file for related issues.

    Thread Starter mainliborg

    (@mainliborg)

    I’ll check debug and get back to you.

    I do find it very odd that the meta field writing is affected since as you state it happens first then the other stuff happens afterward in a different WP table.

    Thread Starter mainliborg

    (@mainliborg)

    No errors being reported in debug. It’s literally the case when the new post mappings is enabled the meta writes stop. And when it’s disabled the meta writes work. Both work properly in what each is supposed to do, but not together.

    Plugin Author Aurovrata Venet

    (@aurovrata)

    try and do everything from the action hook, and set the status to 'future', disable the 'cf7_2_post_status_post' filter

    Thread Starter mainliborg

    (@mainliborg)

    I’ve moved the entire process to the new post mapped function, it now writes the meta fields there when a form is submitted.

    Plugin Author Aurovrata Venet

    (@aurovrata)

    Great! And the post status is set to scheduled publication?

    Thread Starter mainliborg

    (@mainliborg)

    Yes, It both publishes the post and writes the meta data fields using add_post_meta, which I added into the function. I added some of my own code logic that I needed anyway. So while I didn’t resolve the original issue I had, I have it working as I need it.

    Plugin Author Aurovrata Venet

    (@aurovrata)

    So while I didn’t resolve the original issue I had, I have it working as I need it.

    I see. Can you still test it with the UI mapping, and use the final action hook to set the post status to ‘future’ and the post dates to see it works for you?

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Map form input field to post publish date’ is closed to new replies.