post_excerpt, unless the post is * protected. Only works in the Loop. * * @global $post The global post object. */ function relevanssi_the_excerpt() { global $post; if ( ! post_password_required( $post ) ) { echo '

' . $post->post_excerpt . '

'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } else { esc_html_e( 'There is no excerpt because this is a protected post.', 'relevanssi' ); } } /** * Generates an excerpt for a post. * * Takes the excerpt length and type as parameters. These can be omitted, in * which case the values are taken from the 'relevanssi_excerpt_length' and * 'relevanssi_excerpt_type' options respectively. * * @global $post The global post object. * * @param object $t_post The post object. * @param string $query The search query. * @param int $excerpt_length The length of the excerpt, default null. * @param string $excerpt_type Either 'chars' or 'words', default null. * * @return string The created excerpt. */ function relevanssi_do_excerpt( $t_post, $query, $excerpt_length = null, $excerpt_type = null ) { global $post; if ( ! $excerpt_length ) { $excerpt_length = get_option( 'relevanssi_excerpt_length' ); } if ( ! $excerpt_type ) { $excerpt_type = get_option( 'relevanssi_excerpt_type' ); } // Back up the global post object, and replace it with the post we're working on. $old_global_post = null; if ( null !== $post ) { $old_global_post = $post; } $post = $t_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $remove_stopwords = 'body'; /** * Filters the search query before excerpt-building. * * Allows filtering the search query before generating an excerpt. This can * useful if you modifications to the search query, and it may help when working * with stemming. * * @param string $query The search query. */ $query = apply_filters( 'relevanssi_excerpt_query', $query ); $min_word_length = 2; /** * Allows creating one-letter highlights. * * @param boolean Set to true to enable one-letter highlights. */ if ( apply_filters( 'relevanssi_allow_one_letter_highlights', false ) ) { $min_word_length = 1; } $terms = relevanssi_tokenize( $query, $remove_stopwords, $min_word_length ); if ( is_array( $query ) ) { $untokenized_terms = array_filter( $query ); } else { $untokenized_terms = array_filter( explode( ' ', $query ) ); } $untokenized_terms = array_flip( relevanssi_remove_stopwords_from_array( $untokenized_terms ) ); $terms = array_merge( $untokenized_terms, $terms ); // These shortcodes cause problems with Relevanssi excerpts. $problem_shortcodes = array( 'layerslider', 'responsive-flipbook', 'breadcrumb', 'robogallery', 'gravityview', 'wp_show_posts' ); /** * Filters the excerpt-building problem shortcodes. * * Some shortcodes cause problems in Relevanssi excerpt-building. These * shortcodes are disabled before building the excerpt. This filter allows * modifying the list of shortcodes. * * @param array $problem_shortcodes Array of problematic shortcode names. */ $problem_shortcodes = apply_filters( 'relevanssi_disable_shortcodes_excerpt', $problem_shortcodes ); array_walk( $problem_shortcodes, 'remove_shortcode' ); /** * Filters the post content before 'the_content'. * * Filters the post content in excerpt building process before 'the_content' * filter is applied. * * @param string $content The post content. * @param object $post The post object. * @param string $query The search query. */ $content = apply_filters( 'relevanssi_pre_excerpt_content', $post->post_content, $post, $query ); $pattern = get_shortcode_regex( $problem_shortcodes ); $content = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $content ); // Add the custom field content. if ( 'on' === get_option( 'relevanssi_excerpt_custom_fields' ) ) { $content .= relevanssi_get_custom_field_content( $post->ID ); } // Autoembed discovery can really slow down excerpt-building. relevanssi_kill_autoembed(); // This will print out the attachment file name in front of the excerpt, and we // don't want that. remove_filter( 'the_content', 'prepend_attachment' ); /** This filter is documented in wp-includes/post-template.php */ $content = apply_filters( 'the_content', $content ); /** * Filters the post content after 'the_content'. * * Filters the post content in excerpt building process after 'the_content' * filter is applied. * * @param string $content The post content. * @param object $post The post object. * @param string $query The search query. */ $content = apply_filters( 'relevanssi_excerpt_content', $content, $post, $query ); // Removes