You should use add_filter()
instead of apply_filters()
. apply_filters()
is used to apply the filter that’s been registered with add_filter()
. Please check the following code for the solution –
function bbtre_amazon_content_filter( $content ) {
if ( ! is_single() ) {
return $content; // Return early if not single post
}
// Paragraphs array without the closing tag
$paragraphs = explode( '</p>', $content );
// Number of paragraphs in $paragraphs
$paragraphs_count = count( $paragraphs );
// Middle index of $paragraphs
$middle_index = absint( floor( $paragraphs_count / 2 ) );
// New content string
$new_content="";
for ( $i = 0; $i < $paragraphs_count; $i++ ) {
if ( $i === $middle_index ) {
// Add custom content in the middle of post contents
$new_content .= bbtre_the_amazon_items();
}
// Append the missing closing p tag
$new_content .= $paragraphs[ $i ] . '</p>';
}
return $new_content;
}
add_filter( 'the_content', 'bbtre_amazon_content_filter' );