Support » Fixing WordPress » Shortcode enclosed content stripped from excerpt

  • Hi,
    I’m using a plugin to create popups from Wikipedia by enclosing a term in shortcode. Like this

    “Terwijl voormalig [wiki]Goldman Sachs [/wiki]bankier” (text in Gutenberg Paragraph Block, bold by me)

    On a Blog Page and in a Latest Posts Block with non-manual excerpts the following output is rendered:

    “Terwijl voormalig bankier”

    Both the shortcode and the enclosed content are gone (due to wp_trim_excerpt? as called by the_excerpt() ). Whereas content enclosed in HTML tags stays in place.

    The excerpt would preferably read:
    “Terwijl voormalig Goldman Sachs bankier”

    Is there a way to keep the content enclosed by (content enclosing) shortcode tags in the excerpt?

    Using local site with WP 5.3.2 + theme TwentyTwenty + Gutenberg plugin 7.7.1

Viewing 5 replies - 1 through 5 (of 5 total)
  • I suggest that you ask in the support forum for TwentyTwenty
    It’s possible that your theme strips all shortcodes in excerpts and you might want to change that behavior.

    I also found this when I was searching for informatin:

    Thanks for the swift response and your suggestions. In other themes – like Blocksy, Astra – the behavior is the same though. Get the impression it works this way by design (see /wp-includes/shortcodes.php)

    If I create a custom excerpt based on the_content and replace [ with < and ] with > it works fine.

    $mmwb_cont= get_the_content();
    	$mmwb_exc_lengte = 40;
    	$mmwb_cont=str_replace(array("[", "]"), array("<", ">"), $mmwb_cont);
    	return wp_trim_words(excerpt_remove_blocks($mmwb_cont), $mmwb_exc_lengte ,' ' . '&hellip;');

    (Not a viable solution, for illustration only)

    That’s expected behavior for HTML tags.
    But seemingly not for content enclosing shortcode tags.

    One problem that often occurs with excerpts is that if the excerpt is just created as a certain number of characters or words from the content, then the excerpt may contain html tags that aren’t closed.
    In these forums, for instance, we’ve had huge issues with unbalanced li tags. Unbalanced list tags could distort the view of the whole remaining page.
    The easy solution to that is of course to strip all tags from the content before you create the excerpt.

    Shortcodes is another animal. Some shortcodes may output page navigation or adverts, for instance and should never be included in an excerpt. So if the question is yes or no to shortcodes in the excerpt, then “no” is the safest solution. If you need some shortcodes, then you’ll have to do develop a bit for your particular case, typically in a Child Theme.

    I couldn’t agree more where self-closing (shortcode) tags are concerned. But WP itself distinguishes between self-closing and content enclosing shortcode tags:

    “1) The shortcode is a self-closing tag like we seen in the Basic Shortcodes section.
    2) The shortcode is enclosing content.

    Enclosing content with a shortcode allows manipulations on the enclosed content.

    As seen above, all you need to do in order to enclose a section of content is add a beginning [$tag] and an end [/$tag], similar to HTML.”

    A few lines below it reads:
    “The default value of $content is set to null so we can differentiate between a self-closing tag and enclosing tags by using PHP function is_null().”

    (don’t know how that works, not a developer)

    Imho I don’t see why you would want to loose your content (in enclosing shortcode tags) once an excerpt is created containing that content (not similar to HTML).

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Shortcode enclosed content stripped from excerpt’ is closed to new replies.