I have managed to do a test shortcode in my function.php and it does display on my contact 7 form. Oddly it display outside the form tag.
this is my shortcode
function cf7_shortcode()
{ ?>
<span>test shortcode</span>
<?php }
add_shortcode('cf7_shortcode', 'cf7_shortcode');
also i add another line to enable shortcode to be working inside contact form template
add_filter('wpcf7_form_elements', 'do_shortcode');
this is my form template
<label> Nama
[text* nama] </label>
<label> Emel
[email* emel] </label>
<label> No. Telefon
[tel* telefon] </label>
<label> Mesej
[textarea mesej] </label>
<br>
[cf7_shortcode]
[submit "Hantar"]
and this is what happen
I have search on the internet how people do it and it exactly how i do it except my shorcode goes outside the form tag while other people are not. How can i fix this?
I have managed to do a test shortcode in my function.php and it does display on my contact 7 form. Oddly it display outside the form tag.
this is my shortcode
function cf7_shortcode()
{ ?>
<span>test shortcode</span>
<?php }
add_shortcode('cf7_shortcode', 'cf7_shortcode');
also i add another line to enable shortcode to be working inside contact form template
add_filter('wpcf7_form_elements', 'do_shortcode');
this is my form template
<label> Nama
[text* nama] </label>
<label> Emel
[email* emel] </label>
<label> No. Telefon
[tel* telefon] </label>
<label> Mesej
[textarea mesej] </label>
<br>
[cf7_shortcode]
[submit "Hantar"]
and this is what happen
I have search on the internet how people do it and it exactly how i do it except my shorcode goes outside the form tag while other people are not. How can i fix this?
Share Improve this question edited Sep 12, 2019 at 15:52 kadsyy asked Sep 12, 2019 at 15:46 kadsyykadsyy 133 bronze badges1 Answer
Reset to default 2It's because your shortcode echoes the output than returning it — shortcodes should not echo the output, but return
it — unless in certain cases like complex HTML markup in the output, where you need to use output buffering.
So your code should look like:
function cf7_shortcode() {
return '<span>test shortcode</span>';
}
add_shortcode( 'cf7_shortcode', 'cf7_shortcode' );