Question

I am trying to dynamically populate an invisible field in a Gravity form with the WP post title. Right now I have:

<?php echo do_shortcode('[gravityform id="9" field_values="trip_of_interest=the_title();"]'); ?>

However, this ultimately gives me a value of "the_title();", but does not give me the actual title itself.

Edit: The field is now being passed correctly using:

<?php echo do_shortcode('[gravityform id="9" title="false" field_values="trip_of_interest=' . the_title('','',FALSE) . '"]'); ?>

However, if the post title contains an apostrophe, the value that is passed is everything before that apostrophe only, not the complete title.

Was it helpful?

Solution

You can't call a PHP function inside a string like that. Need to use concatenation.

Also you need to set the optional third param to FALSE to return rather than echo the title.

echo do_shortcode('[gravityform id="9" field_values="trip_of_interest=' . the_title('','',FALSE) . '"]'); ?>

OTHER TIPS

I did add this as a comment to jszobody's comment, but thought I would add it here for clarity.

the the_title() function in WordPress echos the title out to the browser - which isn't really the functionality you are after here.

There is a seperate function, get_the_title() which will return the title to be used as part of another string, so the required code would be:

<?php echo do_shortcode('[gravityform id="9" field_values="trip_of_interest=' . get_the_title() . '"]'); ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top