Juned Ansari
Juned Ansari

Reputation: 5275

short code are not working on my custom theme

i am not able to use any shortcode in my custom them is there any thing need to add in functions.php to enable shortcode feature??

i have created simple shortcode like in funxtions.php

function custom_shortcode(){
 echo "hello there !!!";
}

add_shortcode('mycode', 'custom_shortcode');

but when i am trying to use my short code in post it shows simple [mycode] as display output

i dont know whats wrong in that i think i am missing to add something in functions.php for shortcode feature in my cutom theme

Upvotes: 0

Views: 105

Answers (1)

Pablo Demier
Pablo Demier

Reputation: 56

I thing you just need replace "echo" by "return", so:

function custom_shortcode(){
    return "hello there !!!";
}
add_shortcode('mycode', 'custom_shortcode');

"Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from."

From: https://codex.wordpress.org/Function_Reference/add_shortcode

Upvotes: 1

Related Questions