Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Contact form 7 action hook tweak WordPress


Ive made an action hook inside the functions.php of my theme for contact form 7 plugin for Wordpress so that when someone fills out a specific form, it automatically creates a draft post using the details they provide.

However i have only figured out how to use a single form entry field for the post_content, i was wondering if someone could provide the code change required to use the following 3 formdata fields for the "postcontent" section: [time] [location] [webcon]

Here is the code:

function my_wpcf7_save($cfdata) {

$formtitle = $cfdata->title;
$formdata = $cfdata->posted_data;

if ( $formtitle == 'noticeboard') {

// access data from the submitted form
$formfield = $formdata['post'];

// create a new post
$newpost = array( 'post_title' => $formdata['title'],
'post_content' => $formdata['webcon'],
'post_status' => 'draft');

$newpostid = wp_insert_post($newpost);

// add meta data for the new post
add_post_meta($newpostid, 'custom-field1', $formdata['your-name']);


add_action('wpcf7_before_send_mail', 'my_wpcf7_save',1);


As an extra, not as important aspect, if there is a way to get this hook to also take the image they upload and set it as featured image that would be great; if someone can supply that also i will reward extra money

Answers (1)


Arnav Joy answers:

what to you mean by " 3 formdata fields for the "postcontent" section: [time] [location] [webcon]"

can you give an example to help understand you better?

tokenofhon comments:

See this part in the code:
// create a new post

$newpost = array( 'post_title' => $formdata['title'],

<strong>'post_content' => $formdata['webcon'],</strong>

'post_status' => 'draft');

[webcon] is a formsata from the contact form 7, e.g. formfield is Name: and in the backend whatever they enter in that textbox is known as [name] or whatever i decide to call it

In this instance, [webcon] comes from the "enter your content for the website" text area.

the line 'post_content' => $formdata['webcon'],

the php hook grabs the data filled out by the user in the form which has been named [webcon] and posts this in the post content area in wordpress.

what i want to do is grab data from 3 of these formfields and have it put them all in the post content area.

the 3 formfields are [time [location] and [webcon]

tokenofhon comments:


Arnav Joy comments:

you can try it as

$newpost = array( 'post_title' => $formdata['title'],

'post_content' =>$formdata['time'].' '.$formdata['location'].' '. $formdata['webcon'],

'post_status' => 'draft');

tokenofhon comments:

That worked! thankyou so much, been trying to figure this out for days xD

One little thing; is there a way to get it to seperate each one with an enter (&nbsp;) rather than a space?

If not thats fine, it can be done manually in the posts