From f5790a0b11c8d309ae33a76315c3fa4baf0c14a6 Mon Sep 17 00:00:00 2001 From: Aadil Ayub Date: Mon, 1 Apr 2024 15:10:14 +0500 Subject: [PATCH] experiment with adding line breaks in post excerpts --- functions.php | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/functions.php b/functions.php index e059fbe..5857caa 100644 --- a/functions.php +++ b/functions.php @@ -30,4 +30,39 @@ function autonomic_theme_scripts() { // this is also for block styles wp_enqueue_script( 'autonomic-block-styles', get_theme_file_uri( '/js/block-styles.js' ), array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ), filemtime( get_theme_file_uri( '/js/block-styles.js' ) )); } -add_action( 'enqueue_block_editor_assets', 'autonomic_theme_scripts' ); \ No newline at end of file +add_action( 'enqueue_block_editor_assets', 'autonomic_theme_scripts' ); + + +// Code for enabling line breaks in post excerpts + +define('EXCERPT_RARELY', '{[}]'); +define('EXCERPT_BR', nl2br(PHP_EOL)); + +function so306588_trim_excerpt_custom($text = '') +{ + add_filter('the_content', 'so306588_trim_excerpt_custom_mark', 6); + + // get through origin filter + $text = wp_trim_excerpt($text); + + remove_filter('the_content', 'so306588_trim_excerpt_custom_mark', 6); + + return so306588_trim_excerpt_custom_restore($text); +} + +function so306588_trim_excerpt_custom_mark($text) +{ + $text = nl2br($text); + return str_replace(EXCERPT_BR, EXCERPT_RARELY, $text); +} + +function so306588_trim_excerpt_custom_restore($text) +{ + return str_replace(EXCERPT_RARELY, EXCERPT_BR, $text); +} + +// remove default filter +remove_filter('get_the_excerpt', 'wp_trim_excerpt'); + +// add custom filter +add_filter('get_the_excerpt', 'so306588_trim_excerpt_custom'); \ No newline at end of file