I’m utilizing WordPress Underscores theme. I wanted to point out movies inside excerpts on weblog web page. I used to be in a position to modify this operate Permit HTML in excerpt to point out youtube movies inside excerpts like so:
my content material.php makes use of the_excerpt();
my operate.php
operate wpse_allowedtags()
{
// Add customized tags to this string
return '<determine>,<iframe>,<p>,<video>';
}
if (!function_exists('wpse_custom_wp_trim_excerpt')) :
operate wpse_custom_wp_trim_excerpt($wpse_excerpt)
{
$raw_excerpt = $wpse_excerpt;
if ('' == $wpse_excerpt) {
$wpse_excerpt = get_the_content('');
$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*If it's essential enable simply sure tags. Delete if all tags are allowed */
//Set the excerpt phrase depend and solely break after sentence is full.
$excerpt_word_count = 30;
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
$tokens = array();
$excerptOutput="";
$depend = 0;
// Divide the string into tokens; HTML tags, or phrases, adopted by any whitespace
preg_match_all('/(<[^>]+>|[^<>s]+)s*/u', $wpse_excerpt, $tokens);
foreach ($tokens[0] as $token) {
if ($depend >= $excerpt_length && preg_match('/[;?.!]s*$/uS', $token)) {
// Restrict reached, proceed till , ; ? . or ! happen on the finish
$excerptOutput .= trim($token);
break;
}
// Add phrases to finish sentence
$depend++;
// Append what's left of the token
$excerptOutput .= $token;
}
$wpse_excerpt = trim(force_balance_tags($excerptOutput));
$excerpt_end = '...'; /*** MODIFY THIS. change the excerpt endind to one thing else.***/
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
$pos = strrpos($wpse_excerpt, '</');
if ($pos !== false)
// Inside final HTML tag
$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add learn extra subsequent to final phrase */
else
// After the content material
$wpse_excerpt .= $excerpt_more; /*Add learn extra in new paragraph */
return $wpse_excerpt;
}
return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
I do not know a lot of PHP however I used to be in a position to google and perceive some parts of the code above. Nevertheless, I’m not positive I absolutely perceive the next:
- Within the following if situation do these single quotes
''
characterize an empty string? If that’s the case, why do we want an empty string ?
$raw_excerpt = $wpse_excerpt;
if ('' == $wpse_excerpt)
-
Based on https://developer.wordpress.org/reference/features/apply_filters/ the third parameter
$raw_excerpt
within the following operate is$args
which is a required parameter. Why do we have to embody it? What it does precisely within the code above?return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);