-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_excerpt.php
More file actions
55 lines (45 loc) · 1.79 KB
/
replace_excerpt.php
File metadata and controls
55 lines (45 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
// replace "get_the_excerpt" function call with the following code
add_filter( 'get_the_excerpt', function( $excerpt, $post ) {
// Grab the content and set alt_text for the image to be the title of the post
$excerpt = "";
$content = apply_filters('the_content', get_the_content());
$alt_text = get_the_title($post->ID);
// Get the post thumbnail or the first image from the content
$thumbnail_url = get_the_post_thumbnail_url($post, 'thumbnail');
if (!$thumbnail_url) {
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].+alt=[\"](?P<alt>.+?)[\"].+>/i', $content, $matches);
if (isset($matches['src'])) {
$thumbnail_url = $matches['src'];
}
if (isset($matches['alt'])) {
$alt_text = $matches['alt'];
}
}
// Get all content after a specific h3 heading (in this case "Limerick")
// Match the first and second h3 tags
preg_match('/Limerick<\/h3>(.*?)<p><em>/s', $content, $matches);
// If the h3 tags are not found, return a blank paragraph
if (!$matches) {
$paragraphs = "";
} else {
// Extract the content between the h3 tags
$paragraphs = $matches[1];
}
// if we've got a thumbnail, create that as an image with appropriate alt text
if ($thumbnail_url) {
$excerpt .= '<figure class="wp-block-image size-full"><img src="' . $thumbnail_url. '" alt="' . $alt_text . '"></figure>';
}
$excerpt .= $paragraphs;
return $excerpt;
}, 10, 2 );
// workaround to fix https://github.com/lightbulbheaduk/wordpress/issues/1
// with thanks to https://github.com/frzsombor
function fzs_filter_metadata_registration( $metadata ) {
if ($metadata['name'] === 'core/post-excerpt') {
unset($metadata['attributes']['excerptLength']);
}
return $metadata;
};
add_filter( 'block_type_metadata', 'fzs_filter_metadata_registration' );
?>