WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add a comment field (81 posts)

  1. sicktb
    Member
    Posted 9 months ago #

    Im not sure about the cheese and the flowers :)

    All i can tell is that i need like 10 fields on one page.

    How are you fealing today? (example not what its gonna be)
    ///input///

    Do you like cheese? (example not what its gonna be)
    ///input///

    When did you last eat candy? (example not what its gonna be)
    ///input///

  2. bcworkz
    Member
    Posted 9 months ago #

    What I'm trying to determine is who is seeing the input values. You have 6 different forms, which one is used is determined by some criteria. For example: User A has a certain attribute such that he is served form 1. User B has a different attribute so is served form 2. They each fill out the form and submit at approximately the same time. User B's page reloads, showing previously submitted comments. User B sees his just submitted comment. Will user B ever see what user A submitted?

    If no, we're good, if yes, the twentyten_comments() will get rather involved and need to work for all 6 conditions.

  3. sicktb
    Member
    Posted 9 months ago #

    No. User A can not see the comments from user B. And B can not see A.

    But both have the same 6 forms. With the same information.

  4. sicktb
    Member
    Posted 9 months ago #

    i tryed the code that you posted and changed the following test to bcw_test <input type="text" name="bcw_test" id="bcw_test" size="22" />

    And i dont see the following in the template folder: wp-comments-post.php
    As you placed it here: <form action="<?php echo site_url( '/wp-comments-post.php' ); ?>".....

    Im getting the following error when i press on the submit button:
    FAILED: you have no written response.

    I placed the following code in the comments.php for the output to be shown:

    <ol class="commentlist">
    				<?php
    					/* Loop through and list the comments. Tell wp_list_comments()
    					 * to use twentyten_comment() to format the comments.
    					 * If you want to overload this in a child theme then you can
    					 * define twentyten_comment() and that will be used instead.
    					 * See twentyten_comment() in twentyten/functions.php for more.
    					 */
    					wp_list_comments( array( 'callback' => 'twentyten_comment' ) );
    
    					if( current_user_can('like_cheese')) echo 'Favorite cheese? <input type="text" name="bcw_test" id="bcw_test" size="22" />';
    else if( current_user_can('like_flowers')) echo 'Favorite flower? <input type="text" name="bcw_test" id="bcw_test" size="22" />';
    else echo 'What do you like? <input type="text" name="bcw_test" id="bcw_test" size="22" />';
    
    				?>
    				<?php echo get_comment_meta( $comment->comment_ID, 'bcw_test', true ); ?>
    
    			</ol>
  5. bcworkz
    Member
    Posted 9 months ago #

    The added code in your last snippet is in the wrong place, it will not do what it is supposed to do there, as it is outside the <form> tags, so the entered value is not included in the POST data sent with the submit button. It is also outside the comments loop so the values for each comment will not display.

    I see my previous instructions were somewhat flawed, let's try again :( Move this current file you're working on somewhere safe for reference and make a fresh copy of your twentyten theme's comments.php. Be sure your child theme's functions.php page still has the bcw_handle_comment($id) save values function. Don't change anything else just yet except as instructed here. Do not change the input field name from "test", it must be coordinated with the save values function which uses only "test". Do not use any of the if( current_user_can('like_cheese')) code. It has no use in this scheme. Let's get just the basic test field working all the way around. You can add real fields after that.

    Copy the twentyten_comments() function definition to your copy of comments.php, assigning a new function name. Also use this new name in the line wp_list_comments( array( 'callback' => 'twentyten_comment' ) );

    Inside the renamed twentyten_comments() function definition, place this Test: <?php echo get_comment_meta( $comment->comment_ID, 'bcw_test', true ); ?> line above the existing <div class="reply"> line. I added the "Test: " text so you can see where the field is placed on the actual page even if there is no data to display. The 'bcw_test' label is correct here despite 'test' being used elsewhere. It too is coordinated in the save values function.

    Finally, replace the <?php comment_form(); ?> line with this revised version:

    <?php $post_id = get_the_ID();
    	if ( comments_open( $post_id ) ) : ?>
    	<div id="respond" class="comment-respond">
    		<h3 id="reply-title" class="comment-reply-title"><?php comment_form_title(__( 'Leave a Reply' ),__( 'Leave a Reply to %s' )); ?> <small><?php cancel_comment_reply_link(__( 'Cancel reply' )); ?></small></h3>
    		<?php if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) : ?>
    			<?php echo '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>'; ?>
    		<?php else : ?>
    			<form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo 'commentform'; ?>" class="comment-form">
    				<?php $args = '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), get_edit_user_link(), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>';
    				echo apply_filters( 'comment_form_logged_in', $args, $commenter, $user_identity ); ?>
    				<?php $args = '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label> <textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>';
    				echo apply_filters( 'comment_form_field_comment', $args ); ?>
    				Test <input type="text" name="test" id="test" size="22" /><br style="clear: both;"><br>
    				<p class="form-submit">
    					<input name="submit" type="submit" id="<?php echo esc_attr( $args['id_submit'] ); ?>" value="<?php echo __( 'Post Comment' ); ?>" />
    					<?php comment_id_fields( $post_id ); ?>
    				</p>
    			</form>
    		<?php endif; ?>
    	</div><!-- #respond -->
    <?php else :
    	do_action( 'comment_form_comments_closed' );
    endif; ?>

    In my earlier haste, I overlooked some little tricks used in the original code which threw some errors. All tested and working now. This all should yield a functioning test field in the comments.

    FYI, there should not be a wp-comments-post.php template. This file resides in the root WP folder and contains core comment code, it is not a template. It is the code that handles the the comment form data after the submit button is clicked.

    Fingers crossed...

  6. sicktb
    Member
    Posted 9 months ago #

    http://pastebin.com/raw.php?i=jnTtVEL1

    Thats what i have now in the comment.php but when i press on submit it only loads and puts the following at the domain name: test3/#ajaxed-comments

    Fucntion.php i left like this: http://pastebin.com/raw.php?i=ZEfWcVbk

  7. bcworkz
    Member
    Posted 9 months ago #

    On comments.php:
    1. Replace this:
    <?php echo get_comment_meta( $comment->comment_ID, 'bcw_test', true ); ?>

    with this:
    <?php wp_list_comments( array( 'callback' => 'twentyten_comment1' ) ); ?>

    Note that 'twentyten_comment' has a '1' added to the name.

    2. At the very bottom of comments.php type "<?php" (without the quotes)

    Copy the entire function definition for twentyten_comment() from functions.php and place it at the very bottom of comments.php after the "<?php".

    Rename the function 'twentyten_comment' to 'twentyten_comment1'

    Erase the endif; at the very bottom of the copied function definition.

    On functions.php:
    3. The only thing on this page should be:

    //Save the extra comment field value
    <?php function bcw_handle_comment($id) {
       if (array_key_exists('test', $_POST))
          update_comment_meta( $id, 'bcw_test', $_POST['test']);
    }
    add_action('wp_insert_comment', 'bcw_handle_comment');

    Dangerously close to getting this working.

  8. sicktb
    Member
    Posted 9 months ago #

    it gives me this error: Parse error: syntax error, unexpected $end in

    when i add the following code to function.php it will not give me this error but it doesn't work...

    //Save the extra comment field value
    function bcw_handle_comment($id) {
       if (array_key_exists('test', $_POST))
          update_comment_meta( $id, 'bcw_test', $_POST['test']);
    }
    add_action('wp_insert_comment', 'bcw_handle_comment');
    //Override default theme function to display a comment
    
    endif;

    I did add: endif; but than i get this at the adress again: (#ajaxed-comments)

  9. bcworkz
    Member
    Posted 9 months ago #

    You do not want the endif; there. It was the companion to if ( !function_exists('twentyten_comments') ) :. You should not have either line anymore on either page.

    That error usually means there is an imbalance with parenthesis or brackets, there is no such error in the posted snippet. It loads fine when pasted into my functions.php. Where did the error message report the error to be? functions.php or comments.php? What code corresponds to the line number? Note the actual error is sometimes above the quoted line. The quoted line is where the parser figured out there is a problem.

    Imbalanced parenthesis can be hard to find. A programmers editor that highlights parenthesis pairs helps immensely. You still need to move the cursor to each parenthesis in turn and locate it's companion and decide if the indicated pair is appropriate or not. Which means you need some understanding of what the code is supposed to do.

  10. sicktb
    Member
    Posted 9 months ago #

    Parse error: syntax error, unexpected $end in /nfs/home/deb11111/domains/domain.nl/public_html/wp-content/themes/twentyten/functions.php on line 600

    http://pastebin.com/raw.php?i=F8AZmdmX

  11. sicktb
    Member
    Posted 9 months ago #

    I can see that you tested it with a clean copy of the twentyten?

    If this is true then please paste your comment.php and function.php so i can see what i'm doing wrong. I know your learning me but my deadline is almost over. I just need this fields to work. If i can add fields to mutiple comment.php(and it works) than im saved. After that i can test and learn more :)

  12. bcworkz
    Member
    Posted 8 months ago #

    I don't know what's going on with this error. Your version of functions.php works perfectly on my installation. Do you have a clean copy of this file from before we started all of this? This is the only code that should be added to functions.php:

    //Save the extra comment field value
    function bcw_handle_comment($id) {
       if (array_key_exists('test', $_POST))
          update_comment_meta( $id, 'bcw_test', $_POST['test']);
    }
    add_action('wp_insert_comment', 'bcw_handle_comment');

    Nothing else, not even the added code in twentyten_comment().

    All other modifications are on comments.php. The version that's tested and fully functional is in pastebin: comments.php

    I've added notes to make it easy for you to know where to edit in your actual fields. There's two spots on this file, and the field names need to be coordinated with saving the values in bcw_handle_comment().

    Though I used twentyten as a base for the replacement comment form, it was actually tested in my custom child theme based on twentyeleven. To ensure there is no more confusion or errors, the copy in paste bin is a freshly coded version based on twentyten v1.6. I even switched my installation to twentyten theme to fully test. Everything worked flawlessly!

    To summarize, you only need to do two things to get a functional test field. Add the bcw_handle_comment() code to a clean error free copy of functions.php. And replace comments.php with the pastebin version.

    Good luck!

  13. sicktb
    Member
    Posted 8 months ago #

    I did all you named above. I also installed a clean version(1.6) of the theme.

    Fatal error: Cannot redeclare twentyten_comment1() (previously declared in /nfs/home/deb11111/domains/domain.nl/public_html/wp-content/themes/twentyten/comments.php:153) in /nfs/home/deb11111/domains/domain.nl/public_html/wp-content/themes/twentyten/comments.php on line 200

  14. sicktb
    Member
    Posted 8 months ago #

    Ow i copyed some more info.. pastebin is not giving the error above anymore but will not make my output on the page : #ajaxed-comments

    testing some more

  15. sicktb
    Member
    Posted 8 months ago #

    weird think is that i get the new test field but also the original responds field.

    This is the code for the new "clean" function.php: http://pastebin.com/raw.php?i=kVrRfSy6

  16. bcworkz
    Member
    Posted 8 months ago #

    I confirmed your latest functions.php works fine with my pastebin comments.php in the twentyten theme on my installation. Even though it sounds like there's other problems, are you at least seeing the Test: input field and you are able to enter a value and that value is saved and displayed with each related comment?

    If that is the case we've accomplished the main functionality. I don't understand when you say "will not make my output on the page : #ajaxed-comments". What output are you expecting? What is the #ajax-comments page?

    And about "the original responds field"? Is this the large text box where one normally enters comment text? You do not want this? To get rid of it delete line 139 of my pastebin version of comments.php that reads: <div class="comment-body"><?php comment_text(); ?></div>

    You actually can't easily get rid of this because doing so throws errors. What you can do is hide it with CSS entered on styles.css:

    .comment-form-comment {
    	display: none;
    }

    The other problem is if no comment is entered an error is thrown. You can get around this by putting default text in the textarea. Add some text in front of </textarea> near the end of line 87.

    Even with this, any given user can only enter a single comment, after which the comment will be flagged as a duplicate. There's a hook we can use to suppress this, but I'm not sure where it is right now. If this is indeed what you want to do I will take the time to identify the hooks needed to make hiding the textarea work well. Let me know if this is what you want to do.

  17. sicktb
    Member
    Posted 8 months ago #

    Sorry its works now!

    I had a plugin installed that can edit the comment with ajax inline so you dont need to go to the back of wordpress.

    Ajaxed Comments: plugin

    Would have been nice if this plugin still worked with the code we add. This saves the time to make the test field shown in the back of wordpress.

    Do you have eny idee how to edit a comment easly? Just like you can do on this wordpress forum with the edit button on the left?

  18. bcworkz
    Member
    Posted 8 months ago #

    On a rudimentary level, front end editing shouldn't be too hard, but considering how long it took us to get to this point I'm unsure how feasible it would be without extra help.

    The basic approach would be mostly jQuery. When the button is clicked, a pop-up window containing the comment submit form and current HTML values is displayed. Once the user is finished editing, the submit button is clicked and the new form values are sent to the server via AJAX.

    The server first re-confirms the user is the comment author. If so, the new values are inserted in the DB and a response is returned to the client. The client script then transfers the new values to the innerHTML of the main page if a success response is received. If an error is received, an appropriate message is displayed and the page remains as it was.

    It's probably possible to adapt the plugin to work with your custom fields, but it would be a significant project.

  19. sicktb
    Member
    Posted 8 months ago #

    Im trying to get a 2nd field. And its not working. If i just copy the line as below i get 2 times the same answer(makes sens).

    test: <input type="text" name="test" id="test" size="22" /><br style="clear: both;">

    So i tryed the following:

    <!-- !!!!!! Custom form fields start here !!!!!!! -->
    Kaas: <input type="text" name="kaas" id="kaas" size="22" /><br style="clear: both;">Vlees: <input type="text" name="vlees" id="vlees" size="22" /><br style="clear: both;">

    <!-- !!!!!! Custom form field output starts here !!!!!!! -->
    Kaas: <?php echo get_comment_meta( $comment->comment_ID, 'bcw_kaas', true ); ?>Vlees: <?php echo get_comment_meta( $comment->comment_ID, 'bcw_vlees', true ); ?>

    Function this gives me a error: Cannot redeclare bcw_handle_comment() (previously declared in /nfs/home/deb711111/domains/domain.nl/public_html/wp-content/themes/twentyten/functions.php:560) in /nfs/home/deb711111/domains/domain.nl/public_html/wp-content/themes/twentyten/functions.php on line 570

    //Save the extra comment field value kaas
    function bcw_handle_comment($id) {
    if (array_key_exists('kaas', $_POST))
    update_comment_meta( $id, 'bcw_kaas', $_POST['kaas']);
    }
    add_action('wp_insert_comment', 'bcw_handle_comment');

    //Save the extra comment field value vlees
    function bcw_handle_comment($id) {
    if (array_key_exists('vlees', $_POST))
    update_comment_meta( $id, 'bcw_vlees', $_POST['vlees']);
    }
    add_action('wp_insert_comment', 'bcw_handle_comment');

  20. bcworkz
    Member
    Posted 8 months ago #

    You need to save all the various field values in the one function, like so:

    //Save the extra comment field value kaas
    function bcw_handle_comment($id) {
      if (array_key_exists('kaas', $_POST))
        update_comment_meta( $id, 'bcw_kaas', $_POST['kaas']);
      if (array_key_exists('vlees', $_POST))
        update_comment_meta( $id, 'bcw_vlees', $_POST['vlees']);
      //keep adding more if--updates below as needed
    }
    add_action('wp_insert_comment', 'bcw_handle_comment');
  21. sicktb
    Member
    Posted 8 months ago #

    It all works for me. Now im a little bit sad cuz i have to make tons of pages. And than i thought a simple solution.

    If your logged in you can post a comment on a page and this comment can only be shown bij jou and the admin. Not by any other user that logs into the same page?

    This way every one can make their comment on the page without looking to other customers.

  22. bcworkz
    Member
    Posted 8 months ago #

    It doesn't work like that by default. From an earlier comment I had the impression that functionality was handled elsewhere, so I didn't concern myself with it.

    It is possible to implement that behavior. We need to modify the twentyten_comment1() function on comments.php. Don't start making multiple pages yet or you'll need to copy the new code several times over!

    I'm currently traveling, so I can't provide any tested code, but the comment output will need to be wrapped in an if() block that checks if the current user is admin or comment author, only allowing output if one or the other is true. Nothing major, but I can't test until I get home next week.

    In the mean time if you want to see what you come up with, I'll try to check in here once in a while, just not sure when. Sorry I can't be of better help right now.

  23. bcworkz
    Member
    Posted 8 months ago #

    I should also mention the solution proposed above is not very efficient if you have a lot of comments from different users related to any particular post. You are essentially making a big query and then throwing most of the results out.

    The better solution is a little harder to code, it involves altering the comment query so if the current user is not an admin, limit the comments retrieved to only those where the current user is the author.

  24. sicktb
    Member
    Posted 8 months ago #

    Your first post: Thats kinda what i need. I tryed all that i thought that would be good for making this possible. But it's gonna take too long and to much work for my customer to keep track with 600+ pages.

    So im really glad that you tell me that it is possible to view only the comment for 1 customer.

    "The better solution is a little harder to code, it involves altering the comment query so if the current user is not an admin, limit the comments retrieved to only those where the current user is the author."

    I don't really know if this gives me the same result as i'm in need off.

    Can you tell me if this "hard to code" will be with fast result. So i dont need to make 600+ pages for just 100 customers. Just like the first option?

    Let me know if you want to help me with this or please tell me if it has any change to work at all. If not i can easily tell me customer to get to someone else.

    At least i'm really glad that you help me and that i learned what i have learned now.

  25. bcworkz
    Member
    Posted 8 months ago #

    Either method returns the same result -- the only comments seen by normal users are the ones they wrote. Only admins see all comments. And you will not need any more than the initial 6 templates with either approach as well. How many comments from all users can we expect for any give post? If hundreds, we should pursue the more efficient, harder to code option. If there are usually no more than 50-60 comments per post, the easy solution will suffice.

    I'm willing to help as long as you're willing to learn, but time is a factor. We cannot do better than one full exchange per day, look how long it took to get one simple field working! If the low volume comment approach will work for you, I think I could put together a suggested code snippet even from my very limited current resources. I could do this in the next day or two. Untested, so some debugging will likely be required.

    If you have a high volume of comments per post, you really need the altered query approach. For this I would need to use my test installation to even suggest a code snippet. I cannot access that until next week at the earliest.

  26. bcworkz
    Member
    Posted 8 months ago #

    I made an attempt this morning to throw together some of the "easy" code for you. As it turns out, it's not so easy because the comment count and any navigation gets all messed up when the easy method is used. You can see for yourself, here is the easy code:

    //Only allow comment authors to see their own comments (admins see all)
    //Place this snippet after "$GLOBALS['comment'] = $comment;" in twentyten_comment1()
    if( ! current_user_can('moderate_comments')
       && $comment->user_id != get_current_user_id()
    ) { echo 'You do not have permission to view this comment.<br>';
       return;
    }

    I hope everything is correct there, I currently have no way to test.

    For the comment count and navigation to work we have no choice but to alter the comment query. That approach will need to wait until next week.

  27. bcworkz
    Member
    Posted 8 months ago #

    I decided to make an initial attempt at the harder altered query code. There remains much doubt about this that I cannot currently verify, but here it is in case by some miracle it actually works. This is to be used in place of the previous snippet inserted in twentyten_comment1(). While they could safely reside together, the previous code becomes useless extra weight.

    //Alter comment query if not admin to only get current user's comments
    //Place on functions.php
    add_filter('comment_feed_where', 'bcw_current_user_comments');
    function bcw_current_user_comments( $cwhere ) {
      if ( ! current_user_can('moderate_comments')) {
        $user_id = get_current_user_id();
    	$cwhere .= " AND $wpdb->comments.user_id = $user_id";
      }
      return $cwhere;
    }

    As you can see, this doesn't appear "harder" than anything else. What's hard is researching all the ramifications to ensure it works properly, which I haven't done. I'm not even sure this is the right hook. And being weak with SQL, I can't be sure this is the correct modification.

    Still, it wouldn't hurt anything to try it. There's a small chance it will actually work. I'll give it a proper test when I get home in a few days.

  28. sicktb
    Member
    Posted 8 months ago #

    still testing above but, when i try to post 2 times on the same comment it gives me a error about double post.

    This is because i put text in front of the </textarea>

    Can we remove this warning? Not sure if i want to remove it.. Just need to know if it's possible.

  29. sicktb
    Member
    Posted 8 months ago #

    the 2nd peace of code did not do the trick. Can still see the comments from user1 when logged in as user2.

  30. sicktb
    Member
    Posted 8 months ago #

    Option 1 for the comments.php did not work for me...

    function twentyten_comment1( $comment, $args, $depth ) {
                $GLOBALS['comment'] = $comment;
    	         //Only allow comment authors to see their own comments (admins see all)
    //Place this snippet after "$GLOBALS['comment'] = $comment;" in twentyten_comment1()
    if( ! current_user_can('moderate_comments')
       && $comment->user_id != get_current_user_id()
    ) { echo 'You do not have permission to view this comment.<br>';
       return;
    }
    switch ( $comment->comment_type ) :
                        case '' :
                ?>

Reply »

You must log in to post.

About this Topic