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>';

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

1 Answer 1


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>';

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

  • It seems to be working with echo as well. I have changed the quotes, I guess that was the issue! Thank you so much for your answer. May 1, 2019 at 14:28
  • Glad it's sorted, either way. Welcome to the site as well. May 1, 2019 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.