• Resolved Taro

    (@sicktb)


    Dear all,

    Im trying to ad a custom comment field that works the same as the “Message” field. I found all topics and tutorials. But my php skills are not realy updated.

    What did i add? I put ‘test’. This is how i now tryed to edit the comment-template.php

    add_filter('comment_form_default_fields','add_comment_fields');
    
    function comment_form( $args = array(), $post_id = null ) {
    	if ( null === $post_id )
    		$post_id = get_the_ID();
    	else
    		$id = $post_id;
    
    	$commenter = wp_get_current_commenter();
    	$user = wp_get_current_user();
    	$user_identity = $user->exists() ? $user->display_name : '';
    
    	if ( ! isset( $args['format'] ) )
    		$args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml';
    
    	$req      = get_option( 'require_name_email' );
    	$aria_req = ( $req ? " aria-required='true'" : '' );
    	$html5    = 'html5' === $args['format'];
    	$fields   =  array(
    		'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
    		            '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
    		'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
    		            '<input id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
    		'url'    => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' .
    		            '<input id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
    	);
    
    	$required_text = sprintf( ' ' . __('Required fields are marked %s'), '<span class="required">*</span>' );
    	$defaults = array(
    		'fields'               => apply_filters( 'comment_form_default_fields', $fields ),
    		'comment_field'        => '<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>',
    		'test'			=> '<p class="comment-form-comment"><label for="test">' . _x( 'Comment', 'noun' ) . '</label> <textarea id="comment" name="test" cols="45" rows="8" aria-required="true"></textarea></p>',
    		'must_log_in'          => '<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>',
    		'logged_in_as'         => '<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>',
    		'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>',
    		'comment_notes_after'  => '<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
    		'id_form'              => 'commentform',
    		'id_submit'            => 'submit',
    		'title_reply'          => __( 'Leave a Reply' ),
    		'title_reply_to'       => __( 'Leave a Reply to %s' ),
    		'cancel_reply_link'    => __( 'Cancel reply' ),
    		'label_submit'         => __( 'Post Comment' ),
    		'format'               => 'xhtml',
    	);

    And here i edit the ‘test’ in the comments.php from the theme

    <?php $comment_args = array( 'title_reply'=>'Got Something To Say:',
    
    'fields' => apply_filters( 'comment_form_default_fields', array(
    
    'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Your Good Name' ) . '</label> ' . ( $req ? '<span>*</span>' : '' ) .
    
            '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',  
    
        'email'  => '<p class="comment-form-email">' .
    
                    '<label for="email">' . __( 'Your Email Please' ) . '</label> ' .
    
                    ( $req ? '<span>*</span>' : '' ) .
    
                    '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' />'.'</p>',
    
        'url'    => '' ) ),
    
        'comment_field' => '<p>' .
    
                    '<label for="comment">' . __( 'Let us know what you have to say:' ) . '</label>' .
    
                    '<textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea>' .
    
                    '</p>',
    
        'comment_notes_after' => '',
    
            'test' => '<p>' .
    
                    '<label for="test">' . __( 'Hoe maak je kaas?:' ) . '</label>' .
    
                    '<textarea id="test" name="test" cols="45" rows="8" aria-required="true"></textarea>' .
    
                    '</p>',
    
        'comment_notes_after' => '',
    
    );
    
    comment_form($comment_args); ?>

Viewing 15 replies - 61 through 75 (of 80 total)
  • Thread Starter Taro

    (@sicktb)

    That code works like a charm!!!!! That makes me realy happy!

    As for the duplicate post trap, it appears possible to disable it, but it’s not simple. I haven’t tested this, but it appears you would hook ‘comment_duplicate_trigger’ and essentially replicate most of wp_allow_comment(). The context in which the hook appears prevents a simple solution. The easiest work around is probably to automatically enter a timestamp as the default content of the textarea, then hide the field with CSS.

    Im not sure how to get this working…

    Moderator bcworkz

    (@bcworkz)

    That makes me realy happy!

    And that makes me really happy as well!!

    I’ve updated the Pastebin comments.php page with the new code that hides the textarea and inserts a timestamp. The only change is to line 87. This is the new line: <?php $args = '<p class="comment-form-comment" style="display: none;"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label> <textarea id="comment" name="comment" cols="45" rows="8" aria-required="true">' . time() . '</textarea></p>';

    You will see large numbers as comment text on the admin screens, but otherwise this hack is transparent.

    I do believe you can finally start entering real fields on the comment template and into the bcw_handle_comment() function! w00t! Best to do some thorough testing before making copies of the template though. Much easier to fix one template instead of 6.

    I should remind you the field names on the various templates should stay the same on each version if at all possible. You can change the labels seen by the user to anything, but if the names change, it impacts the bcw_handle_comment(). Varying the names significantly complicates things, though it’s still workable if the names absolutely must change.

    Thread Starter Taro

    (@sicktb)

    Option 1:
    Del button, I need this to work for an author(user).

    Option 2:
    I tested this plugin to try and edit the comment. This will only let me edit the timestamp.

    I got a feeling that if i want to make this a 100%…the new comment fields should be placed in the normal comment field… I don’t know if this is possible and if this is the same progress that has to be “done” to add the new fields to the backend?

    Please don’t make any work at option 2 for now! I think option 1 will be fine.

    Thread Starter Taro

    (@sicktb)

    With that pastebin there will still be a timestamp for the user. I dont mind it for admin. For admin this is a easy way to find what post belongs to what user.

    At the moment i did the following <?php /*?><div class=”comment-body”><?php comment_text(); ?></div><?php */?>

    Moderator bcworkz

    (@bcworkz)

    Option 1 is doable, more on that later.

    Option 2. It’s not really feasible to place your new fields in the existing field. Not impossible, but it would be a mess that could easily break. More feasible would be to expand the plugin to recognize your new fields, but doing so would take some investigation and time beyond what I’m willing to do. Additionally, it appears maintenance would be more difficult than it currently is (to add or alter fields in the future)

    So it is something to consider for the future perhaps, but for now, Option 1. Change this line:
    if (current_user_can('edit_post')) {
    to this:
    if ( current_user_can('edit_post') || $comment->user_id == get_current_user_id()) {

    This actually displays the buttons to any of these users: Admins, Editors, the Post Author, the logged in user who wrote the comment.

    And about the timestamp showing to users, a simple oversight on my part. You found the offending code and commented it out. Nice job! It would have been exactly what I would have suggested if I was paying more attention.

    Thread Starter Taro

    (@sicktb)

    And about the timestamp showing to users, a simple oversight on my part. You found the offending code and commented it out. Nice job! It would have been exactly what I would have suggested if I was paying more attention.

    Can i somehow comment it out for everyone except admin?

    And you mean like this:

    Original:

    //Alter comment results if not admin to only have current user's comments
    //Place on functions.php
    add_filter('comments_array', 'bcw_current_user_comments');
    function bcw_current_user_comments( $comments ) {
      if ( current_user_can('edit_post') || $comment->user_id == get_current_user_id()) {
        $comments = array_merge( array_filter( $comments, function( $comment ) {
          if ( $comment->user_id == get_current_user_id() ) return true;
          return;
        }));
      }
      return $comments;
    }

    to:

    //Alter comment results if not admin to only have current user's comments
    //Place on functions.php
    add_filter('comments_array', 'bcw_current_user_comments');
    function bcw_current_user_comments( $comments ) {
      if ( ! current_user_can('moderate_comments')) {
        $comments = array_merge( array_filter( $comments, function( $comment ) {
          if ( $comment->user_id == get_current_user_id() ) return true;
          return;
        }));
      }
      return $comments;
    }

    Thread Starter Taro

    (@sicktb)

    I don’t see any changes with that code. And i don’t see a delete button at the comment. For admin i can see a button where i can change it. But i don’t want this for the user. Only a delete button.

    Moderator bcworkz

    (@bcworkz)

    You can’t really “comment out” for all but admins, but you can put the code in a conditional. Replace the initial /* with if ( current_user_can('moderate_comments')) :

    Replace the terminal */ with endif;

    Please leave the bcw_current_user_comments() function as it was. This removes comments a user is not intended to see, it has nothing to do with the Option 1 delete button feature. (BTW, the “to:” version in your last post appears to be the original. Use the version I first posted last week.)

    The changes I wanted you to make was to the code for Option 1 from wprecipes.com. This change will cause the delete button to show when appropriate.

    Thread Starter Taro

    (@sicktb)

    So it is something to consider for the future perhaps, but for now, Option 1. Change this line:

    if (current_user_can('edit_post')) {
    to this:
    if ( current_user_can('edit_post') || $comment->user_id == get_current_user_id()) {

    Where can i find this?
    Or you mean to put this code in function.php:

    function delete_comment_link($id) {
      if ( current_user_can('edit_post') || $comment->user_id == get_current_user_id()) {
        echo '| <a href="'.admin_url("comment.php?action=cdc&c=$id").'">del</a> ';
        echo '| <a href="'.admin_url("comment.php?action=cdc&dt=spam&c=$id").'">spam</a>';
      }
    }

    Then, edit the comments.php file. Adding the following code. It must be within the comment loop. In the theme, you’ll find a edit_comment_link() declaration. Add the code just after.

    delete_comment_link(get_comment_ID());

    Thread Starter Taro

    (@sicktb)

    I tried the above code. But user1 has not the option to delete comments/ put it to spam. I’m using a plugin to give the user a role(User Role Editor). And this role is only ‘READ’ atm.

    This is maybe why the delete button is not showing. But when logged in as admin i can see the del | spam button. Im not sure what option i have to give to user1 to activate the del/spam button.

    But i don’t like how this buttons work. They transfer me to the backend. Is there a way that this can be in the front? And how to fix it for the user?

    Moderator bcworkz

    (@bcworkz)

    Well, this is embarrassing, I knew the add delete button script was called from the comment loop, but it some how escaped me that it is a called function and not physically in the loop. Which led to a stupid scoping error. My bad, sorry about that.

    The easiest fix, but not best, is to add this line:
    $comment = get_comment($id);
    directly below this:
    function delete_comment_link($id) {

    You should see the delete and spam links where you want them now.

    While I was figuring this out, I noticed another small scoping error with the original code. This is easily fixed by changing the ‘edit_post’ capability in current_user_can() to ‘moderate_comments’. I don’t think you’ll notice any difference with this change.

    The other problem with non-admin users is, though they have the delete link now, it will not work because they do not have moderate_comments capabilities (I assume). I’m also assuming you wouldn’t really want to grant them this capability because they could approve or delete anyone’s comment, not just their own.

    So we need a solution where they can delete only their own comments from the front end. There’s a number of ways to do this. The best is an AJAX implementation. The problem there is it’s pretty much a small coding project that would involve more time than I’m willing to contribute. If you could find someone to code this for you, this would be the best approach.

    That said, I think there may be a relatively simple approach that’s not ideal, but may be good enough. It would involve the delete link linking to the same single post page which is currently being viewed, with some special parameters added instructing the template to delete a particular comment before reloading the page. This should also solve the capability issue. I’ll also see if there’s a better way to add the delete link, right now we’re loading the comment twice due to scoping issues. In addition, there shouldn’t be any reason for a user to mark their own comment as spam, so that link should not appear for normal users.

    I need to investigate further to ensure this scheme will really work, which may take a day or two. I’ll get back to you on this once I have something. In the mean time, ensure the latest changes are working the way you expect. Also, is the timestamp field showing only for admins working correctly?

    Finally, should I remove the spam link for the admin as well as for normal users? It would make things easier, though it can remain if it’s useful. For that matter, is the “Reply” (to a particular comment) button still applicable for your application? Or did you already remove it? I can remove that too if you like.

    Thread Starter Taro

    (@sicktb)

    I did already remove the

    echo ‘| spam‘;

    And i already removed the “Reply”.

    The timestamp is not showing for admins. If the delete button is too much work because of the role manager plugin. Than i need to get this timestamp for admins working.

    Also i found a weird error. But this is maybe because im on the same IP. When i first post in firefox on my admin account. And fast after in IE logged in as user1 i will get a error saying im posting to fast on the same comment. I hope this is only based on IP.

    Below my current Comment.php and function.php. Today i will show my work at the customer and hoping she will like it and dont need much more than i have now.

    Comment.php

    function.php
    When i look in dreamweaver the following code gets a red highlight(looks like a error) but the code works…line 574 and 577

    I kinda edit it all to view all correct at the customer.

    Moderator bcworkz

    (@bcworkz)

    About the red code, I can only imagine it does not like the anonymous function. It’s an illegal construct in older versions of PHP. We could define a named function and pass it as a parameter to array_filter() but there’s little point. If someone’s version of PHP is that old, I believe they will have other issues with WordPress as well.

    The posting too fast thing is to prevent script driven comment floods which could result in dozens of spam messages every second. It works off either IP or email being the same and the last comment being less than 15 seconds old default, though plugins can extend this. It will not fire for admins, so if you test user1 first, you’re less likely to have the error fire by the time you return to user1, where if you go the other way, the admin post “counts” because of the same IP.

    Here are my revisions to the most recent files you posted:
    functions.php
    comments.php

    These changes should have things working the way you want, try them out and see. I hope you get these in time, but either way, I hope your client presentation goes well, good luck!

    Thread Starter Taro

    (@sicktb)

    Ok customer was really happy. And liked it as it is and dont need the timestamp… So i dont know if you fixed much on the new files? Cuz than i probably don’t need to change my current.

    Some options that are requested by the customer:

    1) Comments should be editable in the frontend by the user. I gonna try some ajaxed plugins. This used to work with the early stage of the code. Can you tell me if this is hard to get to working?

    2) In the role manager plugin i need to ad the user to get read only. If i dont put read on thy user is not allowed to view the site. But the user is now allowed to change there password, name etc. I don’t want that the user is able to change this in the back…

    Thats it…

    Thread Starter Taro

    (@sicktb)

    Simple add the following lines in function.php will not give me a edit button. And it probably will only give me the option to edit the timestamp.

    $subscriber= get_role(‘subscriber’);
    $subscriber->add_cap(‘edit_comment’);

Viewing 15 replies - 61 through 75 (of 80 total)
  • The topic ‘Add a comment field’ is closed to new replies.