A WordPress excerpt without HTML tags is what most card layouts, meta descriptions and builder templates need: plain text, a fixed number of words, no stray <p>, links or shortcode output, and no theme “Read more” link glued to the end.
This snippet prints exactly that for the current post. It uses the manual excerpt when the post has one and otherwise the post content, removes blocks’ markup, shortcodes and tags, and cuts it to the number of words you set.
The snippet
<?php
/**
* Plain-text excerpt of the current post: no HTML, no shortcodes, a fixed word count.
* Scripts Organizer: Trigger location Conditions, Script location Shortcode.
*/
$dpc_words = 30; // Maximum number of words.
$dpc_more = '…'; // Appended when the text was cut. '' for nothing.
$dpc_excerpt_post = get_post();
if ( $dpc_excerpt_post instanceof WP_Post && ! post_password_required( $dpc_excerpt_post ) ) {
// The manual excerpt if there is one, otherwise the content.
$dpc_text = has_excerpt( $dpc_excerpt_post ) ? $dpc_excerpt_post->post_excerpt : $dpc_excerpt_post->post_content;
$dpc_text = excerpt_remove_blocks( strip_shortcodes( $dpc_text ) );
$dpc_text = wp_trim_words( $dpc_text, $dpc_words, $dpc_more ); // Also strips all tags.
if ( '' !== $dpc_text ) {
echo '<p class="dpc-excerpt">' . esc_html( html_entity_decode( $dpc_text, ENT_QUOTES, 'UTF-8' ) ) . '</p>';
}
}
Add it with Scripts Organizer
In Scripts Organizer go to Code Blocks → Add New and give the block a name. Set Trigger location to Conditions, tick Shortcode under Script location (leave PHP unticked), paste the code into the PHP editor, keep Template on All and publish.
The block now shows its shortcode, for example [scorg_shortcode id="123"]. Paste it where the output should appear: a Shortcode block in the post or in a block theme template, or the shortcode element of your page builder’s single or archive template.
Inside a Query Loop or a builder repeater the shortcode prints the excerpt of each post in the loop, on a single post template the excerpt of that post. Scripts Organizer includes this file every time the shortcode renders, inside its own function. That is why the code has no function declarations (a second shortcode on the same page would fatal with “Cannot redeclare”), uses get_the_ID() or get_queried_object_id() instead of a bare $post, and prefixes its variables with dpc_.
How it works
get_post()is the post of the current loop iteration, so it works on single templates and inside loops. Password protected posts print nothing, like WordPress’ own excerpt.- The text is read from the raw
post_excerptorpost_content. It does not callget_the_excerpt(), which runs the excerpt filters and would add your theme’s “Continue reading” text. strip_shortcodes()andexcerpt_remove_blocks()drop shortcodes and blocks that don’t belong in an excerpt,wp_trim_words()strips every tag and HTML comment and cuts to$dpc_wordswords, adding$dpc_moreonly when it actually cut something.- The result is decoded and escaped once with
esc_html(), so&in the content prints as&and never as&amp;.
Customise it
- Length: change
$dpc_words. To change the length of the normal WordPress excerpt everywhere instead, see Change the default excerpt length. - Ending: set
$dpc_moreto''for none, or' ...'. To turn the default[…]into a link, see [Change the excerpt […] to a read more link](https://code.dplugins.com/change-default-excerpt/). - Different wrapper: change the
pand itsdpc-excerptclass in theecholine.
Gotchas
- Text that blocks render dynamically, such as a Query Loop or a synced pattern inside the post, is not in
post_contentand does not appear in the excerpt. wp_trim_words()counts words by spaces. For Chinese, Japanese or Thai it counts characters instead, which WordPress decides from the site language.
In the Scripts Organizer library Import it with one click from Scripts Organizer.