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

  • Resolved nemo-maritime

    (@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 - 16 through 30 (of 60 total)
  • Thread Starter nemo-maritime

    (@nemo-maritime)

    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.

    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’.

    Thread Starter nemo-maritime

    (@nemo-maritime)

    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.

    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…

    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.

    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…

    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.

    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.

    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…

    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…

    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. 😉

    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…

    🙂

    Sweet. Good show!

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

    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…

    To add, in case you get stuck figuring out how to add it to each comment in a theme that uses wp_list_comments…

    You’ll need to create a custom callback for the comments..

    Then do the jiggery pokery in the theme functions.php…

    What this guy wrote seems quite helpful… and i think he’s onto something regarding the regex…

    http://www.russellbeattie.com/blog/mobile-browser-detection-in-php

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