Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add excerpt to results from Google Analytics Top Content Widget
<?php
/**
* Add the post excerpt to the results in the Google Analytics Top Content Widget.
*
* @param string $item_html The item html.
* @param array $page The page result object from Google Analytics.
* @param WP_Post|null $wppost The WordPress post object (if we have it).
*
* @return string The modified item html.
*/
function gtc_list_item_add_excerpt( $item_html, $page, $wppost ) {
$excerpt = '';
if ( isset( $wppost->ID ) ) {
if ( ! empty( $wppost->post_excerpt ) ) {
$excerpt = wp_kses_post( $wppost->post_excerpt );
} elseif ( ! empty( $wppost->post_content ) ) {
$text = strip_shortcodes( $wppost->post_content );
/** This filter is documented in wp-includes/post-template.php */
$text = apply_filters( 'the_content', $text );
$text = str_replace(']]>', ']]&gt;', $text);
/**
* Filters the number of words in an excerpt.
*
* @since 2.7.0
*
* @param int $number The number of words. Default 55.
*/
$excerpt_length = apply_filters( 'excerpt_length', 55 );
/**
* Filters the string in the "more" link displayed after a trimmed excerpt.
*
* @since 2.9.0
*
* @param string $more_string The string shown within the more link.
*/
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
$excerpt = wp_kses_post( wp_trim_words( $text, $excerpt_length, $excerpt_more ) );
}
}
// Make a place for the excerpt
$format = str_replace( '</li>', $excerpt . '</li>', $format );
return $item;
}
add_filter( 'gtc_list_item', 'gtc_list_item_add_excerpt', 10, 6 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.