Skip Navigation

[Resolved] Gravity Forms Hook to custom post types

This support ticket is created 9 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 5.27 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 10:00 – 19:00 -
- - - - - - -

Supporter timezone: Europe/Madrid (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by Brent 9 years, 2 months ago.

Assigned support staff: Caridad.

Author
Posts
#205385

Hello I need help with creating a custom hook that will submit multiple donations posts to an existing custom post type with one form submission. Donations is the custom post type names.

If you view this form I'm trying to code out a form that allows a user to select a rider and then a donation amount. Then if they want to donate to another rider they can click a + button and it will show another rider select field and another donation amount.

Ideally I need each additional Rider/donation (group) to post as a new donation post to my Donations custom post type and associate the selected rider as the donations parent. This will allow me to associate a rider to a donation for a rider profile view. So I need to add logic in my hook to test if these additional fields have values so it can then create a new donation post for them and assign a parent rider for each additional donation.

Some questions I have in regards to hooks. I need to be able to possibly add some if then statements and logic, is this possible using a hook?

I need to test to see if a value exists within a GF field on the same form before creating a new post for my donations post type, can you provide an example of how this might be accomplished with a hook?

Any help would be appreciated as there is not a lot to reference online for this. Or any links that relate to creating gravity form hooks that submit to custom post types.

hidden link

Please help!

#205628

Dear Brent,

There is not much difference between submitting a custom post type or a normal post, just the post_type parameter.

Having this in mind, you can follow any instructions you find for gravityforms with very little adjustment.

On the other hand, we have our CRED plugin that we can help you better with.

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Regards,
Caridad

#205836

No worries, I know this isn't really the right forum for gravity forms help, they seem to be lacking in that area. I created this solution and added to my functions file and so far it works great. Here is the form i'm using. hidden link

So it will create from 1 to 5 donation posts depending the users additional donations form selection.
Thanks for the help Caridad, your the best!

<?php
add_action("gform_after_submission_2", "create_donation_content", 10, 2);

function create_donation_content($entry, $form){
//getting post
$post = get_post($entry["post_id"]);
//adding post args array and setting to donations post type
$donation_post_args = array(
'comment_status' => 'closed', // or open
'ping_status' => 'closed',
'post_author' => 1, // this represents admin by default.
// might need to pull user info if is real user

'post_title' => $entry[59] . ' ' . $entry[60], // this will get the donors name

'post_status' => 'publish',
'post_type' => 'donation',
);

//Start Additional Donation 2 logic
//need to test if 2nd donation field has a value before creating a new post, this donation field id is 12
$greenlight = $entry[12];
if($greenlight == ""){

} else {
//inserting a new donation
$post_id = wp_insert_post( $donation_post_args );

// add more meta values if you need
$donation_meta_values = array(
'wpcf-donation-1' => $entry[12],
'wpcf-donation-rider-id' => $entry[8],
'wpcf-donation-anonymous' => $entry["65"],
//fields below will not change
'wpcf-donor-first-name' => $entry[59],
'wpcf-donor-last-name' => $entry[60],
'wpcf-donor-email' => $entry[64],
'wpcf-donor-phone' => $entry[62],
'wpcf-donor-address' => $entry["19.1"] . '
' . $entry["19.3"] . '
' .$entry["19.4"] . '
' . $entry["19.5"] . '
' . $entry["19.6"]
);
// as long as wp_insert_post didnt fail...
if($post_id > 0){
foreach($donation_meta_values as $key => $value) {
// add our post meta
update_post_meta($post_id, $key, $value);
}
}
//updating post
wp_update_post($post);

}//end if test

// End Additional Donation 2 logic////////////////////////////////////

//Start Additional Donation 3 logic
//need to test if 3nd donation field has a value before creating a new post, this donation field id is 25
$greenlight3 = $entry[25];
if($greenlight3 == ""){

} else {
//inserting a new donation
$post_id = wp_insert_post( $donation_post_args );

// add more meta values if you need
$donation_meta_values = array(
'wpcf-donation-1' => $entry[25],
'wpcf-donation-rider-id' => $entry[24],
'wpcf-donation-anonymous' => $entry["68"],
//fields below will not change
'wpcf-donor-first-name' => $entry[59],
'wpcf-donor-last-name' => $entry[60],
'wpcf-donor-email' => $entry[64],
'wpcf-donor-phone' => $entry[62],
'wpcf-donor-address' => $entry["19.1"] . '
' . $entry["19.3"] . '
' .$entry["19.4"] . '
' . $entry["19.5"] . '
' . $entry["19.6"]
);
// as long as wp_insert_post didnt fail...
if($post_id > 0){
foreach($donation_meta_values as $key => $value) {
// add our post meta
update_post_meta($post_id, $key, $value);
}
}
//updating post
wp_update_post($post);

}//end if test

// End Additional Donation 3 logic

//Start Additional Donation 4 logic
//need to test if 4th donation field has a value before creating a new post, this donation field id is 29
$greenlight4 = $entry[29];
if($greenlight4 == ""){

} else {
//inserting a new donation
$post_id = wp_insert_post( $donation_post_args );

// add more meta values if you need
$donation_meta_values = array(
'wpcf-donation-1' => $entry[29],
'wpcf-donation-rider-id' => $entry[27],
'wpcf-donation-anonymous' => $entry["69"],
//fields below will not change
'wpcf-donor-first-name' => $entry[59],
'wpcf-donor-last-name' => $entry[60],
'wpcf-donor-email' => $entry[64],
'wpcf-donor-phone' => $entry[62],
'wpcf-donor-address' => $entry["19.1"] . '
' . $entry["19.3"] . '
' .$entry["19.4"] . '
' . $entry["19.5"] . '
' . $entry["19.6"]
);
// as long as wp_insert_post didnt fail...
if($post_id > 0){
foreach($donation_meta_values as $key => $value) {
// add our post meta
update_post_meta($post_id, $key, $value);
}
}
//updating post
wp_update_post($post);

}//end if test

// End Additional Donation 4 logic

//Start Additional Donation 5 logic
//need to test if 4th donation field has a value before creating a new post, this donation field id is 29
$greenlight5 = $entry[33];
if($greenlight5 == ""){

} else {
//inserting a new donation
$post_id = wp_insert_post( $donation_post_args );

// add more meta values if you need
$donation_meta_values = array(
'wpcf-donation-1' => $entry[33],
'wpcf-donation-rider-id' => $entry[32],
'wpcf-donation-anonymous' => $entry["70"],
//fields below will not change
'wpcf-donor-first-name' => $entry[59],
'wpcf-donor-last-name' => $entry[60],
'wpcf-donor-email' => $entry[64],
'wpcf-donor-phone' => $entry[62],
'wpcf-donor-address' => $entry["19.1"] . '
' . $entry["19.3"] . '
' .$entry["19.4"] . '
' . $entry["19.5"] . '
' . $entry["19.6"]
);
// as long as wp_insert_post didnt fail...
if($post_id > 0){
foreach($donation_meta_values as $key => $value) {
// add our post meta
update_post_meta($post_id, $key, $value);
}
}
//updating post
wp_update_post($post);

}//end if test

// End Additional Donation 5 logic

};// End add_action