Troubleshooting Common Code Snippets Shortcode Issues
Content
Using shortcodes with the Code Snippets plugin is a powerful way to add dynamic functionality to your WordPress site. However, users often encounter a few common problems that prevent their shortcodes from working as expected. This guide covers the most frequent issues and their solutions.
1. Shortcode Outputs Raw Text Instead of Executing
This is one of the most common issues. You place a shortcode like [code_snippet id=4] on a page, but instead of seeing the dynamic result, you just see the shortcode text itself.
Why this happens: This is almost always a caching issue. Many WordPress sites use caching plugins (like LiteSpeed Cache) or server-level caching to improve performance. These systems save a static version of your page, which means PHP code, including shortcodes, isn't executed on every page load.
Solution: Clear your site's cache after updating a snippet. If the problem persists, you may need to configure your caching plugin to exclude the specific page where the shortcode is used from being cached. The 'Code Snippets' team suggests using the sgo_exclude_urls_from_cache filter for the SG Optimizer plugin, for example.
2. Unwanted Paragraph Tags and Line Breaks in Output
Your shortcode works, but it's wrapped in extra <p> tags or has unexpected line breaks, which can break your layout.
Why this happens: WordPress's automatic formatting system, wpautop, is designed to convert line breaks into paragraphs. It can sometimes interfere with HTML output from shortcodes.
Solution: When inserting a snippet's shortcode, remove the 'format' attribute. For example, use [code_snippet id=33 php] instead of [code_snippet id=33 php format]. The 'format' attribute explicitly tells WordPress to apply paragraph formatting, which is often not desired for HTML snippets.
3. Shortcode Attributes Not Being Passed Correctly
You create a shortcode that accepts parameters, like [my_shortcode color="blue"], but your function doesn't receive the values.
Why this happens: The shortcode callback function must be written to accept the $atts parameter. A common mistake is defining a function that doesn't accept any parameters.
Solution: Ensure your shortcode function is properly defined. Here is the correct structure:
add_shortcode( 'my_shortcode', function ( $atts ) {
$args = shortcode_atts( array(
'color' => 'red', // default value
), $atts );
return "The color is: " . $args['color'];
} );
4. The "Evaluate Shortcodes" Checkbox Unchecks Itself
You try to enable the option to evaluate shortcodes within an HTML snippet, but it automatically disables when you save.
Why this happens: This typically occurs if there is a syntax error or an unsupported shortcode within the snippet's content that prevents it from being saved with the setting enabled.
Solution: Carefully check the code in your HTML snippet for any errors. Temporarily remove any complex shortcodes to see if the setting will save. If it does, you know the problem lies with the shortcode you removed.
5. Basic Shortcode Mistakes
Sometimes the issue is a simple typo in the code.
- Missing href attribute: A shortcode returning a link like
<a "https://example.com">Link</a>will fail. It must be<a href="https://example.com">Link</a>. - Echoing instead of Returning: A shortcode function must
returnits output, notechoit directly. If you need to output complex HTML, use output buffering:add_shortcode( 'my_shortcode', function () { ob_start(); // Start output buffering ?> <!-- Your HTML content here --> <?php return ob_get_clean(); // Return the buffered output } );
By following these troubleshooting steps, you should be able to resolve the majority of shortcode issues encountered with the Code Snippets plugin.
Related Support Threads Support
-
Shortcode html should not be wrapped in taghttps://wordpress.org/support/topic/shortcode-html-should-not-be-wrapped-in-tag/
-
Shortcode resolves on Elementor preview but not live frontendhttps://wordpress.org/support/topic/shortcode-resolves-on-elementor-preview-but-not-live-frontend/
-
Code snippet executes twicehttps://wordpress.org/support/topic/code-snippet-executes-twice/
-
shortcode inside phphttps://wordpress.org/support/topic/shortcode-inside-php/
-
Cant use 2 of the same snippethttps://wordpress.org/support/topic/cant-use-2-of-the-same-snippet/
-
I want to include an url i define between the shortcode tags.https://wordpress.org/support/topic/i-want-to-include-an-url-i-define-between-the-shortcode-tags/
-
Shortcode inside of shortcodehttps://wordpress.org/support/topic/shortcode-inside-of-shortcode-2/
-
Not Shortcodehttps://wordpress.org/support/topic/not-shortcode/
-
Place my snippet on specific part of pagehttps://wordpress.org/support/topic/place-my-snippet-on-specific-part-of-page/
-
code snippets attributes failhttps://wordpress.org/support/topic/code-snippets-attributes-fail/
-
shortcode not workinghttps://wordpress.org/support/topic/shortcode-not-working-376/
-
Struggling to use a global variablehttps://wordpress.org/support/topic/struggling-to-use-a-global-variable/
-
Button Shortcode with Post Varshttps://wordpress.org/support/topic/button-shortcode-with-post-vars/
-
Shortcode for php snippet not workinghttps://wordpress.org/support/topic/shortcode-for-php-snippet-not-working/
-
Reload shortcode contenthttps://wordpress.org/support/topic/reload-shortcode-content/
-
Add Code Snippet to format Author Bio?https://wordpress.org/support/topic/add-code-snippet-to-format-author-bio/
-
Extra “?php”https://wordpress.org/support/topic/extra-php/
-
Help with shortcodehttps://wordpress.org/support/topic/help-with-shortcode-6/
-
Passing a ‘function argument’ through the shortcodehttps://wordpress.org/support/topic/passing-a-function-argument-through-the-shortcode/
-
How to use “Evaluate additional shortcode tags”?https://wordpress.org/support/topic/how-to-use-evaluate-additional-shortcode-tags/
-
example HTML shortcodehttps://wordpress.org/support/topic/example-html-shortcode/
-
PHP not executing – only displays the code on the pagehttps://wordpress.org/support/topic/php-not-executing-only-displays-the-code-on-the-page/
-
Insert Year HTML Snippet, Remove line breakhttps://wordpress.org/support/topic/insert-year-html-snippet-remove-line-break/
-
Evaluate additional shortcode tags deselects after saving a snippethttps://wordpress.org/support/topic/evaluate-additional-shortcode-tags-deselects-after-saving-a-snippet/
-
Code Snippet fails with attributeshttps://wordpress.org/support/topic/code-snippet-fails-with-attributes/
-
Example HTML shortcode not workinghttps://wordpress.org/support/topic/example-html-shortcode-not-working/
-
Pass variable to add_shortcode functionshttps://wordpress.org/support/topic/pass-variable-to-add_shortcode-functions/
-
possible to have php shorcode in the flux of a page?https://wordpress.org/support/topic/possible-to-have-php-shorcode-in-the-flux-of-a-page/
-
JSON error with a shortcode on a pagehttps://wordpress.org/support/topic/json-error-with-a-shortcode-on-a-page/
-
how i can create shortcode insidehttps://wordpress.org/support/topic/how-i-can-create-shortcode-inside/
-
What about shortcode?https://wordpress.org/support/topic/what-about-shortcode/
-
Snippet shortcodehttps://wordpress.org/support/topic/snippet-shortcode/
-
Shortcode not executedhttps://wordpress.org/support/topic/shortcode-not-executed-2/
-
Can’t figure out how to create a shortcode with a basic linkhttps://wordpress.org/support/topic/cant-figure-out-how-to-create-a-shortcode-with-a-basic-link/
-
Pass a custom parameter in the shortcode and use that as variable insidehttps://wordpress.org/support/topic/pass-a-custom-parameter-in-the-shortcode-and-use-that-as-variable-inside/
-
Internal 500 Errorhttps://wordpress.org/support/topic/internal-500-error-12/
-
Variable in shortcodehttps://wordpress.org/support/topic/variable-in-shortcode-3/
-
Execute Shortcode In Header Code Snippethttps://wordpress.org/support/topic/execute-shortcode-in-header-code-snippet/
-
add shortcode to plugin phphttps://wordpress.org/support/topic/add-shortcode-to-plugin-php/
-
How to add a shortcode to front page?https://wordpress.org/support/topic/how-to-add-a-shortcode-to-front-page/
-
embedding shortcode in custom HTMLhttps://wordpress.org/support/topic/embedding-shortcode-in-custom-html/
-
the snippet does not load dynamicallyhttps://wordpress.org/support/topic/the-snippet-does-not-load-dynamically/
-
Nesting a shortcode inside a HTML snippethttps://wordpress.org/support/topic/nesting-a-shortcode-inside-a-html-snippet/
-
How to display Learndash Course Description in shortcodehttps://wordpress.org/support/topic/how-to-display-learndash-course-description-in-shortcode/
-
Add variable to shortcodehttps://wordpress.org/support/topic/add-variable-to-shortcode/
-
Create shortcode from php snippethttps://wordpress.org/support/topic/create-shortcode-from-php-snippet/
-
Adding parameters to shortcodehttps://wordpress.org/support/topic/adding-parameters-to-shortcode-2/
-
Convert HTML Script to Shortcodehttps://wordpress.org/support/topic/convert-html-script-to-shortcode/
-
Cache and current time snippethttps://wordpress.org/support/topic/cache-and-current-time-snippet/