Last active
January 17, 2017 03:21
-
-
Save jtsternberg/65f581cc1c3788aa2654adbb357fe88c to your computer and use it in GitHub Desktop.
Add excerpt to results from Google Analytics Top Content Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(']]>', ']]>', $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', ' ' . '[…]' ); | |
$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