Search code examples
phpwordpressshortcodegravity

How to pass Wordpress the_title(); tag through Gravity Form shortcode


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.


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) . '"]'); ?>