Skip to content

Commit 0b78c64

Browse files
committed
Replace specific matched block instead of replacing all blocks
1 parent f8f67c2 commit 0b78c64

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

lib/blocks.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ function do_blocks( $content ) {
115115
preg_match_all( $matcher, $content, $matches, PREG_OFFSET_CAPTURE );
116116

117117
$new_content = $content;
118+
$offset_differential = 0;
118119
foreach ( $matches[0] as $index => $block_match ) {
119120
$block_name = $matches['block_name'][ $index ][0];
120121

@@ -132,7 +133,15 @@ function do_blocks( $content ) {
132133
}
133134

134135
// Replace the matched block with the static or dynamic output.
135-
$new_content = str_replace( $block_match[0], $output, $new_content );
136+
$new_content = substr_replace(
137+
$new_content,
138+
$output,
139+
$block_match[1] - $offset_differential,
140+
strlen( $block_match[0] )
141+
);
142+
143+
// Update offset for the next replacement.
144+
$offset_differential += strlen( $block_match[0] ) - strlen( $output );
136145
}
137146

138147
return $new_content;

0 commit comments

Comments
 (0)