Here's one approach using the following custom input arguments:
wp_list_comments( [
'_inject_period' => 5,
'_inject_content' => [ 'AAA', 'BBB', 'CCC' ],
] );
where we setup the inject period and define the content to inject.
This could be supported by the following demo plugin that extends the \Walker_Comment
class and enhances the start_el()
method:
<?php
/**
* Plugin Name: Comment Content Injector
* Plugin URI: https://wordpress.stackexchange.com/a/251247/26350
*/
add_filter( 'wp_list_comments_args', function( $args )
{
// Period - Validation
if(
! isset( $args['_inject_period'] )
|| ! is_int( $args['_inject_period'] )
|| 0 === $args['_inject_period']
)
return $args;
// Content - Validation
if(
! isset( $args['_inject_content'] )
|| ! is_array( $args['_inject_content'] )
)
return $args;
// Custom Walker
$args['walker'] = new class extends \Walker_Comment
{
public function start_el( &$output, $comment, $depth=0, $args=array(), $id=0 )
{
static $instance = 0;
static $key = 0;
$tag = ( 'div' == $args['style'] ) ? 'div' : 'li'; // see Walker_Comment
// Inject custom content periodically
if(
0 == ++$instance % absint( $args['_inject_period'] )
&& isset( $args['_inject_content'][$key] )
) {
$output .= sprintf(
'<%s class="injected">%s</%s>',
$tag,
$args['_inject_content'][$key],
$tag
);
$key++;
}
// Parent method
parent::start_el( $output, $comment, $depth, $args, $id );
}
};
return $args;
} );
We might also want to adjust this to a given comment depth by checking the $depth
variable. We could also inject it after the parent method but currently it's before.
Note that here we use an anonymous class. Here's more info on how to extend it without it.
Another approach would be to use the comments_array
filter or if you use a custom callback, adjust it to your needs.
Hope you can adjust it further to your needs!