John Davidson

php - Save Custom Post Type Title in Gravity Form Hidden Field

0 comments
Message:


I am trying to save the custom post type title with gravity form.Currently each post on careers page have form in it so when a user submit the form it should save the custom post type title as well.Each post is showing as accordion and each accordion have gravity form.I would like to know how do i save that current accordion post tile with form submission.Please check the screenshot for better understanding.




My Answer:
To save the custom post type title in a Gravity Form hidden field, you can use the gform_field_value filter hook. Here's an example code snippet that you can add to your theme's functions.php file:

php
add_filter( 'gform_field_value_custom_post_title', 'populate_custom_post_title' );
function populate_custom_post_title( $value ) {
global $post;

// Check if the current post is a custom post type
if ( get_post_type() == 'your_custom_post_type' ) {
$value = get_the_title( $post->ID );
}

return $value;
}


In this code snippet, replace 'your_custom_post_type' with the slug of your custom post type. This code will populate the hidden field with the custom post type title when the form is submitted from a post of that custom post type.

Next, you need to add a hidden field to your Gravity Form with the parameter name set to 'custom_post_title'. This hidden field will be populated with the custom post type title when the form is submitted.

After adding the hidden field to your form and adding the code snippet to your functions.php file, the custom post type title should be saved with the form submission.

Rate this post

3 of 5 based on 8311 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us