WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Automatically adding text to comment after comment is submitted (61 posts)

  1. nemo-maritime
    Member
    Posted 5 years ago #

    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.

  2. nemo-maritime
    Member
    Posted 5 years ago #

    Any ideas on how to do this?

  3. asechrest
    Member
    Posted 5 years ago #

    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. =)

  4. nemo-maritime
    Member
    Posted 5 years ago #

    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.

  5. nemo-maritime
    Member
    Posted 5 years ago #

    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.

  6. nemo-maritime
    Member
    Posted 5 years ago #

    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.

  7. nemo-maritime
    Member
    Posted 5 years ago #

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

  8. asechrest
    Member
    Posted 5 years ago #

    Here's the comment_text hook.

  9. asechrest
    Member
    Posted 5 years ago #

    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. ;)

  10. asechrest
    Member
    Posted 5 years ago #

    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.

  11. asechrest
    Member
    Posted 5 years ago #

    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.

  12. nemo-maritime
    Member
    Posted 5 years ago #

    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.

  13. nemo-maritime
    Member
    Posted 5 years ago #

    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.

  14. asechrest
    Member
    Posted 5 years ago #

    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. ;-)

  15. t31os
    Member
    Posted 5 years ago #

    Not that it's crucial, but you could change these lines...

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

    for..

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

    or...

    $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... :-)

  16. asechrest
    Member
    Posted 5 years ago #

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

    @nemo-maritime

    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.

  17. nemo-maritime
    Member
    Posted 5 years ago #

    Exactly. So that after the comment is made through the mobile version, it shows on the comment whether it is viewed from the mobile site or the regular site, but so the extra text can only be added from the mobile site. You have it perfectly.

    The plugin I am using is MobilePress, found at http://wordpress.org/extend/plugins/mobilepress/ it has two different files, one for regular mobile phones, and another for the iphone, each with its own settings, but also shared core files.

  18. asechrest
    Member
    Posted 5 years ago #

    Ok, nemo-maritime.

    About the best I could do for you is a workaround that only functioned for you. What I mean is that you could create a separate "mobile" login name and post comments from your mobile phone using that login, and I could write a filter that appended the text only to that login's comments.

    But I think the ideal solution -- detecting when a comment is made from the mobile version and then remembering that the comment was made from the mobile and displaying as such in the future no matter which version of the site you're using -- is a bit beyond my current coding ability.

    Perhaps t3los, stvwlf, Michaelh, or any of the other coding gurus around here have some ideas.

    Sorry I couldn't get it done for ya'.

  19. nemo-maritime
    Member
    Posted 5 years ago #

    Hey, you did great. Thank you for all of your help. I originally thought I could just add some code that will automatically add predefined text when someone is using the mobile version to the end of their comment but I almost went crazy trying to figure it out ;). At least you tried, and that counts for a lot, as you gave someone an idea somewhere. I'll keep digging around but I doubt I will find anything, most likely someone close to your knowledge will get this, as I will not, but I keep practicing. Again, thank you very much for your time and effort in trying to figure this out.

  20. Mark / t31os
    Moderator
    Posted 5 years ago #

    I'm only a hobby coder, by no means a guru, lol....

    I'd say the first thing you need to focus on is how to determine when a user is posting from a mobile device...

    Start here, and run some tests ...
    Google search for mobile related scripts.

    Once you have a script that's not huge and suits the task you just need a way to hook the check to the comment submission...

    I don't have a mobile that can browse, and i've not worked with mobile code, but it should be a matter of combining a script with a hook and job done...

    If such a feature is important you may wish to consider hiring a coder.
    http://jobs.wordpress.net/

    From a coding point of view, the check should be made when retrieving data from the database. You may also need to additionally store some information when a comment is submitted... hard to say, but i'm pretty sure it will straight-forward enough for anyone who has done this kinda thing before...

  21. asechrest
    Member
    Posted 5 years ago #

    I looked at storing some sort of marker in the database but I've never coded that type of thing so it's just a bit beyond me at this point.

    As far as I can tell, MobilePress should actually do the job of determining if a user is accessing from a mobile device -- it even passes this status through variables. And it does some things with it's own database tables. But as far as I could tell it doesn't maintain any connection between this status and a comment made under that status.

    Which, according to my newbie coding brain, meant I had no easy way (like checking for comment_author or comment_ID, etc.) to track a comment made from a mobile.

  22. Mark / t31os
    Moderator
    Posted 5 years ago #

    In "wp_comments", the user agent is stored as "comment_agent" ...

    wp_ being my test install database prefix (yours may differ)...

    If you know which user agents to match (google will cover that), then i think you could just add bits when comments are being retrieved..

    Something like... (this is not intended as working code, this is purely an example snippet of code)

    <?php if($comment->comment_agent == 'some_mobile_agent') { ?>
    <div class="mobile_post">This message was posted from a mobile device.</div>
    <? }

    Could proberly be plonked straight in the theme.

    Pretty sure the row from the comments table should be available to grab info from.. ... you'll need to go through the codex...

  23. asechrest
    Member
    Posted 5 years ago #

    Am I thinking about this right? You could come at this two ways.

    Either:

    1) You need to "mark" (storing in the database) a comment made from the mobile phone and then use a filter to add the text to any "marked" comments. (I don't know anything about storing to database -- would love some resources or examples.)

    --OR--

    2) You need actually change the comment text in the database at the time of posting from the mobile, and in this way avoid having to "mark" the comment for the filter.

    I have a feeling #2 would not be recommended, but I'm not sure.

  24. asechrest
    Member
    Posted 5 years ago #

    Ahhh, nice t31os. You posted while I was writing mine.

    I'm going to take a look at that tonight. Looks like a great idea.

    Would love to get this working just for my own experience.

  25. Mark / t31os
    Moderator
    Posted 5 years ago #

    No, you don't need to add anything, that's my point.

    I took a look at my local install and it seems the commenters User Agent is already getting stored when a comment is made...

    So now it's only a matter of creating a check on the value of this row in the database and printing out what you want when it matches a particular value...

    Here's the value of one of the entries from my test install..
    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6

    So obviously we know that comment was submitted by someone using Firefox and a Windows PC...

  26. Mark / t31os
    Moderator
    Posted 5 years ago #

    lol, you posted when i was to...

    Pretty sure it should be easy to check the User Agent...

    Of course the only thing to be concerned about is when a User Agent comes up with something odd or incorrect... and of course it can be spoofed...

    For the most part most users won't know what that means and will have a valid user agent to check against...

  27. asechrest
    Member
    Posted 5 years ago #

    Well, I may give it a go starting tonight. Sort of new territory for me so we'll see how it goes.

    Thanks for the advice t31os. I'll post back if I hit a snag. ;)

  28. Mark / t31os
    Moderator
    Posted 5 years ago #

    I've just quickly tested and you can get the commenters user agent in the theme comments.php file...

    <?php print $comment->comment_agent; ?>

    Will print for example..

    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10

    So imagine that's a string, all we need to do is check various parts of the string, and match that against known mobile agents..

    If we get a match then echo/print/return an additional paragraph, div or whatever element we want...

    :)

  29. asechrest
    Member
    Posted 5 years ago #

    Sweet. Good show!

    Now to brush up on my regex, which I never really learned properly in the first place! ;-)

  30. Mark / t31os
    Moderator
    Posted 5 years ago #

    Oh, don't feel too left out, regex gives me a headache......

    Well not literally... :)

    preg_match should be sufficient...

    You don't need to be exact with the matches ...

    For example if the value contains "firefox" and "mozilla" and "windows" , i think you can safely assume that it's a regular vistor and not a mobile...

    EDIT: had a look at the wordpress comment file...it's just using the regular PHP..
    $_SERVER['HTTP_USER_AGENT'];

    To obtain the user agent...

    So if you want to emulate the value that will be in the database rows, you can use the above in your testing...

Topic Closed

This topic has been closed to new replies.

About this Topic