Support » Fixing WordPress » Automatically adding text to comment after comment is submitted

  • Resolved nemo-maritime


    I was wondering how I can add text to a comment after it has been submitted. Kind of like a signature. What I want is to have the comment field look normal when someone is posting, and have the text field empty at first. Then after they post the comment, a block of text would be automatically added a paragraph or two below their comment, but in their comment area. If that makes sense. So have the comment field empty when someone is typing, and after they submit their comment, a block of text gets added a line or two below their comment text, but still in the same comment.

Viewing 15 replies - 1 through 15 (of 60 total)
  • Any ideas on how to do this?

    greenshady wrote a filter to preset text in the new post/page editor.

    I would guess you could use a filter for comments as well. I’m not sure whether it would prefill the textarea or append it to the comment prior to posting, or if a filter could be written to do either/or.

    The coding is a bit out of my league. Maybe greenshady will come across this thread. =)

    I saw the article, interesting. I wonder if something like that can be ported over and adapted for the comments field. That is something of what I am looking for. Just need something to add text automatically to comments after they are submitted.

    He said that no, no it cant. To also look for something to the extent of “comment_text filter hook”. Being new to php I am not sure where to head to now.

    I got a short fix. I just added the text to be pre-filled into the box when a comment is being filled out. That is really bad looking as the text is just there when the page loads and it looks weird and very un-professional.

    I ended up talking to the guy who wrote the article but he cant at this time. Any advanced WordPress users who might know?

    Here’s the comment_text hook.

    I can try to write the filter but it won’t be until next week. And no promises that I can get it to work. 😉

    Alright, I had some time to mess around tonight and I came up with this. Be aware, I’ve done only minutes of testing and this appeared to work on two test comments, and also appends the text to an edited comment. I’m by no means a coder, so consider it an “alpha” version. Let me know how it works, and h/t to greenshady for the original filter (and WordPress for the awesome hooks).

    Add the following to functions.php.

    add_filter( 'comment_text', 'my_comment_text' );
    function my_comment_text( $mytext ) {
    	global $comment;
    	$mytext = get_comment_text( $comment ) . 'Test comment text!';
    	return $mytext;

    You can style it a bit with XHTML if you so desire. For example you can add a couple of <br’s> before the ‘T’ in ‘Test’ if you want the added text to be down a few lines from the main content.

    Let me know if this works for you, and if not, in what situations it doesn’t.

    [EDIT] – FYI this is a site-wide change. This will be appended to all comments. It also appears to break the formatting of the WP-Syntax plugin. I’ll have to do some more research to figure out if I can do it per-comment instead of site-wide.

    I wrote up a guide for implementing the filter and the example in the guide has some XHTML text styling to help illustrate how the “signature” could be styled.

    Hope this helps anyone who desires this functionality.

    If anyone has an alternate or cleaner method, please post here or above.

    THANK YOU, THANK YOU, THANK YOU!!!!!! Man, I can just keep going with that. I just got back to this post and found a nice surprise. Thank you very much, I will be checking this out right now and see how it works and will be back to let you know. Man, you deserve a donation, but with me being a little broke for the moment, I will have to “AdSense you” – click two or three “relevant” AdSense ads in a few minutes apart interval. Again, thank you, I will take a look and let you know in a little bit how it worked out.

    Ahhhh, alright. Almost got where I was aiming for. Almost. That added the text automatically, and that is perfect, but I am having an issue.

    Ok, so the main reason I needed this whole extra feature was because I also have a mobile version of my site, and I am looking for a way to automatically add a signature to the comment that comes from the mobile version of the site. So I added the code to the functions.php of the mobile plugin, and that added the text to the bottom, and it added it to all of the comments automatically. The issue I had was that it at first showed up only when looked at from the mobile version of the site. It did not show up on the regular site, that is not the mobile version.

    Second thing I tried was adding it to the main functions.php file of the plugin. The plugin has two, one for the master settings of the plugin, and another functions.php for the theme being used. So I added it to the master functions.php file of the plugin and this time it put the signature on all of the comments. Again, this is half way, because not all of the comments were made from the mobile site.

    So this got me to almost where I needed to be. The thing I would require is that the text gets added automatically after the the comment is submitted from the mobile version of the site, then show that it was made from the mobile version of the site on both the mobile version, as well as the regular site.

    My logic for getting this would be to add something somehow to the functions.php file, or some other file, of the mobile theme. This way when a comment is made from the mobile version of the site, the extra text would be added to the comment, but it would display the comment regularly in both the mobile version as well as the full version of the site. The text gets added only if the comment is made using the mobile plugins comment.php file, or whatever file I need to modify.

    If that whole ramble of mine makes much sense. Thank you very much for your work though, it started me off in a right direction and got me a whole lot closer.

    Hey, Nemo. I responded to your comment on my site.

    I’ll try to dig into this this weekend.

    P.S. – greenshady deserves some credit, too. He got me into filters. 😉

    Not that it’s crucial, but you could change these lines…

    $mytext = get_comment_text( $comment ) . 'Test comment text!';
    	return $mytext;


    $mytext = 'Test comment text!';
    	return get_comment_text( $comment ) . $mytext;


    $mytext = get_comment_text( $comment );
    	$mytext .= 'Test comment text!';
    	return $mytext;

    Just to throw around a couple of slightly different approaches to the same thing… 🙂

    Thanks, T3los. I like that second one, it’s cleaner.


    See if I have this right. You want the text to be added only to the comments you make the comment from your mobile (i.e. comments made using the mobile plugin), and you want that text viewable whether you’re using the mobile plugin or not, right?

    Could you provide a link to the mobile plugin? I’ll do my best but may have to defer to the PHP wizards here in the forum. The filter I wrote is basically a site-wide change, so figuring out a per-comment solution could be tougher.

Viewing 15 replies - 1 through 15 (of 60 total)
  • The topic ‘Automatically adding text to comment after comment is submitted’ is closed to new replies.