Skip to content

Commit

Permalink
Merge pull request #22 from helsingborg-stad/fix/do-not-index-body-of…
Browse files Browse the repository at this point in the history
…-tags

fix: do not include contents of some tags from source.
  • Loading branch information
sebastianthulin authored Feb 10, 2025
2 parents 11dcbb0 + fa594dc commit 768dde9
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion source/php/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private static function getPost($post)
'ID' => $post->ID,
'post_title' => apply_filters('the_title', $post->post_title),
'post_excerpt' => self::getTheExcerpt($post),
'content' => strip_tags(apply_filters('the_content', $post->post_content)),
'content' => self::stripTags(apply_filters('the_content', $post->post_content)),
'permalink' => get_permalink($post->ID),
'post_date' => strtotime($post->post_date),
'post_date_formatted' => date(get_option('date_format'), strtotime($post->post_date)),
Expand Down Expand Up @@ -352,6 +352,21 @@ private static function getPost($post)
return null;
}

public static function stripTags($content) {
$removeBodyOfTags = [
'script',
'style',
'noscript'
];

$content = preg_replace(sprintf(
'/<(%s)\b[^>]*>.*?<\/\1>/is',
implode('|', $removeBodyOfTags)
), '', $content);

return strip_tags($content);
}

public static function getTheExcerpt($post, int $numberOfWords = 55) {

$excerpt = get_the_excerpt($post);
Expand Down

0 comments on commit 768dde9

Please sign in to comment.