0

Do you have any idea of is it possible to add some content every let’s say 5 comments in WordPress? I could use “more fields” plugin but it would create content after each and every comment. Something like replies containing advertisements, which occurs every 5 forum replies in PhpBB but for WordPress.

e.g.

- comment 1  
- comment 2  
- ...  
- comment 5  
- MY CONTENT    
- comment 6  
- ... 
- comment 10  
- MY CONTENT
- comment 11

1 Answer 1

1

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!

10
  • Hi, I would like to thank you so much for this. Unfortunately I am getting something like this: Parse error: syntax error, unexpected 'class' (T_CLASS) in /home/gonzonst/public_html/wp-content/plugins/comment-content-injector/index.php on line 25 and I can't activate the plugin. I simply replaced wp list comments inside comments.php and created index.php file where I pasted second part of your code. Than I placed php file inside folder called comment-content-injector and placed it inside plugins folder. Is that corect?
    – Michal K
    Commented Jan 4, 2017 at 10:43
  • Most likely your PHP doesn't support the anonymous class. You can e.g. check out my answer here how to adjust it accordingly. @user110187
    – birgire
    Commented Jan 4, 2017 at 10:55
  • OK, I got this ;) One more tip - I want to use custom fields inside comments.php file so that I get <?php the_field('custom_title'); ?> in place of AAA here - '_inject_content' => [ 'AAA', 'BBB', 'CCC' ],. How? Sorry, I'm noob in terms of php
    – Michal K
    Commented Jan 4, 2017 at 11:24
  • Looks like you're using ACF, I think the_field() function echos the value, so you could try get_field() instead or just try the get_post_meta() core function. @user110187
    – birgire
    Commented Jan 4, 2017 at 11:40
  • erghh not working. Do you have any idea what's wrong here: '_inject_content' => [ '<?php the_field('content1'); ?>', 'BBB', 'CCC' ], or paste your example but exactly?
    – Michal K
    Commented Jan 4, 2017 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.