最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Contact form 7 shortcode appear outside form tag

programmeradmin2浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 2

It'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' );
发布评论

评论列表(0)

  1. 暂无评论