Make WordPress Core

Opened 11 years ago

Last modified 7 years ago

#23634 assigned enhancement

New hook for adding content after each comment

Reported by: lancewillett's profile lancewillett Owned by: chriscct7's profile chriscct7
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Comments Keywords: has-patch
Focuses: Cc:


Similar to #18561 (which is for a new "after post" hook) add a hook that fires after each comment output with wp_list_comments.

Attachments (4)

23634.diff (517 bytes) - added by codeclarified 11 years ago.
23634.2.diff (635 bytes) - added by MikeHansenMe 9 years ago.
23634.3.diff (711 bytes) - added by MikeHansenMe 9 years ago.
with Docs
23634.4.patch (2.6 KB) - added by keesiemeijer 7 years ago.
after_comment filter

Download all attachments as: .zip

Change History (23)

11 years ago

#1 @codeclarified
11 years ago

  • Cc codeclarified added
  • Keywords has-patch added; needs-patch removed

Added a hook to class-wp-walker.php - due to the callback functions on comments this was the only reliable location I could find to add such a hook without worrying about it being dropped due to a custom callback, and still keeping a nested reply from duplicating the content after the levels start closing

#2 @mordauk
11 years ago

  • Cc pippin@… added

#3 @sunnyratilal
11 years ago

  • Cc sunnyratilal5@… added

#4 @DrewAPicture
11 years ago

  • Keywords punt added

#5 @MikeHansenMe
11 years ago

  • Cc mdhansen@… added

#6 @ocean90
11 years ago

  • Milestone changed from 3.6 to Future Release

#7 @SergeyBiryukov
10 years ago

  • Keywords punt removed

#8 @chriscct7
9 years ago

  • Keywords needs-refresh added
  • Owner set to chriscct7
  • Status changed from new to assigned

9 years ago

#9 @MikeHansenMe
9 years ago

  • Keywords needs-refresh removed

#10 @chriscct7
9 years ago

  • Milestone changed from Future Release to 4.4
  • Status changed from assigned to accepted

Thanks @mikehansenme

#11 @DrewAPicture
9 years ago

  • Keywords needs-docs added

@MikeHansenMe We'll need a hook doc to go with the action proposed in 23634.2.diff.

#12 @MikeHansenMe
9 years ago

Completely forgot about that.. my bad. I will get it added. Thanks @DrewAPicture.

9 years ago

with Docs

#13 @MikeHansenMe
9 years ago

  • Keywords needs-docs removed

#14 @DrewAPicture
9 years ago

  • Owner changed from chriscct7 to DrewAPicture

Thanks @MikeHansenMe, I'll take a look.

#15 @DrewAPicture
9 years ago

I'm not sure about the placement of the hook. In comment:1 @codeclarified mentioned that it was the only reliable place to put it, and if that's the case, then this really doesn't fire after a comment has been displayed.

#16 @DrewAPicture
9 years ago

  • Owner changed from DrewAPicture to chriscct7
  • Status changed from accepted to assigned

#17 @wonderboymusic
9 years ago

  • Milestone changed from 4.4 to Future Release

#18 @lukecavanagh
8 years ago

Okay if you apply the patch from @MikeHansenMe and add a echo "Hello Name";. It will just echo out on the parent itself rather then displaying on the child. So it seems like adding in a after_element action into class-wp-walker.php woukld make more sense. So that it could be used for menus and comments etc. Thoughts?

Version 1, edited 8 years ago by lukecavanagh (previous) (next) (diff)

7 years ago

after_comment filter

#19 @keesiemeijer
7 years ago

As a proof of concept patch 23634.4.patch doesn't add the hook inside the WP_WALKER class. It does add a method start_el_after that the child classes can use to add content after the element. Because this method is called right after the start_el method you can insert content before the end tag (and child elements if they exist).

<ol class="comment-list">
	<li id="comment-1" class="comment">
			<article id="div-comment-31" class="comment-body"> ... </article>

			<!-- Comment after is inserted here. -->

			<ol class="children"> ... </ol><!-- #child elements-## -->
	</li><!-- #comment-## -->

I've added the start_el_after method to the Walker_Comment class with a filter to add content. Now you can use the filter like this:

add_filter( 'after_comment', 'add_content_after_comment', 10, 3 );

function add_content_after_comment( $comment, $depth, $args ) {
        return $comment . '<p>Inserted content after comment.</p>';

An alternative is to ditch the extra method (in WP_WALKER) and add the filter directly in the start_el method of the Walker_Comment class.

Note: See TracTickets for help on using tickets.