php - Shortcode from a function not working

I am trying to create a shortcode from a custom function in WP but it seems like I'm doing something wrong. I have

I am trying to create a shortcode from a custom function in WP but it seems like I'm doing something wrong.

I have a function in child's theme function.php and add_shortcode code:

function uniquetestfunction() {
echo '<div class="my_class">Test Element</div>';
}
add_shortcode(‘uniqueshortcodename’,’uniquetestfunction’); 

Which should generate a shortcode [uniqueshortcodename]. I have done this with existing functions in different plugins and it worked fine.

But when added to a page, it's not rendering the function, just showing the shortcode as a text. Any ideas what I'm doing wrong?

I'm very new to PHP :) Thanks

I am trying to create a shortcode from a custom function in WP but it seems like I'm doing something wrong.

I have a function in child's theme function.php and add_shortcode code:

function uniquetestfunction() {
echo '<div class="my_class">Test Element</div>';
}
add_shortcode(‘uniqueshortcodename’,’uniquetestfunction’); 

Which should generate a shortcode [uniqueshortcodename]. I have done this with existing functions in different plugins and it worked fine.

But when added to a page, it's not rendering the function, just showing the shortcode as a text. Any ideas what I'm doing wrong?

I'm very new to PHP :) Thanks

Share Improve this question asked May 1, 2019 at 13:48 jade newportjade newport 251 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

If you refer to the documentation for add_shortcode, you need your function to return the content:

function uniquetestfunction($attributes) {
   return '<div class="my_class">Test Element</div>';
}
add_shortcode('uniqueshortcodename','uniquetestfunction');

You also used the wrong quotes in your add_shortcode line - stick with single ' or double " quotes.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745535172a4631872.html

相关推荐

  • php - Shortcode from a function not working

    I am trying to create a shortcode from a custom function in WP but it seems like I'm doing something wrong. I have

    9小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信