Custom shortcodes not working using __s theme

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.

Solutions Collecting From Web of "Custom shortcodes not working using __s theme"

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

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.