I'm trying to register a new shortcode on my custom theme.

In functions.php I have created a basic test function as follows

function get_second_image($atts) {
    echo ‘test’;

And registered it right underneath

add_shortcode( ‘case_study_second_image_sc’, ‘get_second_image’ );

Using the WYSIWYG editor I insert the shortcode [case_study_second_image_sc] but it just displays as the raw code on the page.

I'm calling the page content using

<?php the_content(); ?>

in the template file.

Based on info from another SE question I have searched my theme's files for any add or remove filter functions but there don't appear to be any that would be getting in the way.


Make sure to use straight quotes, e.g. ' or " and not curly quotes , , , or for strings.

Also, shortcodes should return their output, not echo it.

When WordPress finds a shortcode without a callback, it displays the shortcode tag just as it was entered e.g. [case_study_second_image_sc].

The shortcode tag ‘case_study_second_image_sc’ and the associated callback ‘get_second_image’ will be parsed as constants by PHP. This means we won't have a valid shortcode tag or callback function for our shortcode and WP will just output the shortcode as it was entered by the user.

Turning on error reporting helps to find these tricky errors (sometimes curly quotes appear in copy/pasted code and it can be difficult to differentiate quotes in some editors).

Use of undefined constant ‘case_study_second_image_sc’ - assumed '‘case_study_second_image_sc’'

Use of undefined constant ‘get_second_image’ - assumed '‘get_second_image’'
  • 1
    Thank you, the issue was with the type of quote used. I must have copy/pasted in a slanted quote from somewhere by accident.
    – S. Coll
    Jul 12 '17 at 18:08

See the documentation here https://codex.wordpress.org/Shortcode_API. Your function needs to return the value you want to replace the shortcode. The 'echo' will not display anything. You need to return 'test'; to insert the text 'test' in the content.

From the docs in the above link (emphasis added):

When the_content is displayed, the shortcode API will parse any registered shortcodes such as "[myshortcode]", separate and parse the attributes and content, if any, and pass them the corresponding shortcode handler function. Any string returned (not echoed) by the shortcode handler will be inserted into the post body in place of the shortcode itself.

Hope this helps.

  • You are right that the function should return a value for proper implementation, however I was merely echoing a string as a test. In this case it was not the use of echo that was causing the issue.
    – S. Coll
    Jul 12 '17 at 18:11
  • Even if you 'echo' the value, you may not see the echo string. Other page stuff may overwrite where you expect to see the echo'd string. If you really wanted to see the echo'd string, you should put a die() statement just after the echo. Jul 18 '17 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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