I have a function like so (this is in a WordPress site's functions.php, with Gravity Forms):
function my_function() {
return '<aside>Some text, and then I want to include a gravity form. [how do i do that here?] </aside>';
}
I've also tried something like this:
function my_function() { ?>
<aside>Some text, and then I want to include a gravity form. <?php gravity_form(1, false, false, false, '', true); ?></aside>
<?php } ?>
The second works, in that it properly calls the gravity form and displays my function on the front end of the site, but it just spits it out at the beginning of the page (or in my case, I'm using this all in a shortcode, so it spits it out in the beginning of the_content()
).
What am I doing wrong?
gravity_form()
echoes (outputs) the form instead of returning, which is what you want it to do.
You can use output buffering to catch that output and then return it as a string from a function.
function my_function() {
ob_start();
?>
<aside>Some text, and then I want to include a gravity form. <?php gravity_form(1, false, false, false, '', true); ?></aside>
<?php
return ob_get_clean();
}
Since you are using your function as a shortcode, you want to return
the output (the former) and not echo
it (the latter).
Use output buffering to capture the output of gravity_form()
and include it in your return value.
function my_gf_shortcode() {
ob_start();
gravity_form( $form_id, false, false, false, '', false ); // call the proper gravity form you want to display
$gravity_form = ob_get_contents();
ob_end_clean();
return "<aside>Some text, and then I want to include a gravity form. $gravity_form </aside>";
}
when you finalize php code ?> and type hmtl codes , its shows where you type them.So it splits them. You can try this one
function myfunction($sometext) {
$otherfunction_returns=other_function(1,1,1,1);
return $sometext.$otherfunction_returns;
}