• 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 - 16 through 30 (of 80 total)
  • Thread Starter Taro

    (@sicktb)

    Thx bcworkz that works like a charm.

    I tryed to customize some information but i think i need some facts if you can tell me. I hope the below is posibble in some way.

    I need to do the following exemple:

    1) Make: 10 logins(customers)
    2) Make: 6 pages a person (totaal 60 pages)
    3) Make: custom fields for this 6 pages

    I was thinking to do this my makeing this custom fields in the function.php and put the correct fields in a comment.php. This way i simple need to make 6x a diffrent comment.php. And include the correct comment.php this will replace the <?php comments_template(); ?>.

    Seems this isnt realy how it works. I can place the code from the function to the comment.php. This way i can edit it on the normal comment.php just like in the function.php. The error will come when i try to add <?php include ‘comment.php’; ?>.

    Code function.php now:
    Code comment.php now: https://pastebin.com/raw.php?i=j4Gnbwm2

    Moderator bcworkz

    (@bcworkz)

    If I understand correctly, each customer’s comment section on their pages have unique custom fields in the comment form, and the values saved from those fields are displayed in some unique way for each customer.

    You can cause different comment templates to be loaded by using the ‘comments_template’ filter and returning the path to the correct template. And you could simply put a custom form on the template instead of calling comment_form(). Which negates the need to hook into comment form actions that I suggested previously.

    My instinct tells me this is not a good approach though. I would try storing whatever is different from customer to customer in user meta under a common key name. Then your single comment template can get that unique content by getting it out of user meta using the same key each time. No need to generate multiple variations of the template and manage which one gets loaded for which user.

    Even if that doesn’t work because I don’t understand what’s unique, I believe there must be a way to do this without generating multiple templates.

    Thread Starter Taro

    (@sicktb)

    Ill try to explain it beter. Cus i dont realy understand how this “user meta” is working.

    Totaal 10(will be more) users that can login to the website with his own login information.

    They will only see a standaard menu. This menu will be the same for all the user that will login. Cuz im gonna try to put all the pages on 1 layout and use the scroll function Go to page one. This will be linked on the custom title of that page.

    At the backand i have to create 6 pages per person. However all these pages will have the same template. This way all the comment field will be the same for every user and the menu will always work. I only have to set (with a plugin) that those 6 pages can be shown by a specific user. This way thy will not see all the other 6x pages that are on the website.

    But if im correct im gonna get a problem with the <?php echo get_comment_meta( $comment->comment_ID, ‘bcw_test’, true ); ?>

    If user1 posts a comment on his page than user2 will see this comment ?

    To change the comment_form or poiting out the following?: Comments_template_Filter

    Moderator bcworkz

    (@bcworkz)

    Thanks for that, I see now I’m confusing template pages (the PHP page/file) with WP pages (the post type “Page”). I’m sure there will not be a problem with inappropriate visibility on the front end. Comment meta is related to a specific comment, the value stored is only retrieved when a particular comment is being displayed. In turn, a particular comment is only displayed if it is associated with a particular post. By inheritance, the meta value is only used when the grandparent post is being displayed.

    Yes you can filter the comment template being loaded, but I still don’t think it’s necessary. Any unique content can be stored in the DB and only displayed to users that have the right to see it. You know your situation far better than me, I still may not be getting it. If your scheme works, then it’s all good. But I hope to save you needless work and make your system efficient. OTOH, following an approach you do not understand can be more trouble than it’s worth.

    On the backend, I’m not so sure about inappropriate visibility. This applies not only to comment meta, but comments themselves. (Unless you code some provision, comment meta is not visible on the backend anyway) If the user has permission to view the comments screen, they will see all comments in the system, not just theirs… I think. I’ve no experience with segregating access to comments.

    You should be able to test this now with the test field. Add a comment as user1 to user1’s page. Login as user2 and see what you can see. Since you can’t see user1’s post, you can’t see the comment nor the test field. But can you see user1 comments on the backend? Or can you not see any comments? Only user2 comments? I’m not sure, so test!

    On a related note, you could probably hook into the add new user process and if adding a particular role, automatically create 6 pages for the user. You could even add a metabox to the add new user screen where you could enter the titles for each page or any other data that is unique and cannot be scripted. Not sure exactly what’s involved, but it should be possible.

    Thread Starter Taro

    (@sicktb)

    I got it almoost to working. I can now add custom fields to the reply field in a comment.php and this way i can make custom templates that i can load for specific pages.

    But i have the add field function in the function.php this adds the field below the reply. If i whant to put this on every comment.php it will give me a error.

    This: comment.php is what i have for a comment.php that will show the correct field in the correct places.

    And this code: function.php is still located in the function.php that makes the field and places it in the comment place.

    I have to get this code from function.php and place it in the comment.php whit out getting errors…

    Putting the code <?php echo get_comment_meta( $comment->comment_ID, ‘bcw_test’, true ); ?> in the comment.php works… But that only works for the first post. If i want to post a new comment the line will disapear and will only be show in the new comment.

    Moderator bcworkz

    (@bcworkz)

    I may be misunderstanding what you’re really trying to do.

    First, the echo get_comment_meta() line that displays the value. You say it’s in the correct place on comment.php. But it only shows once after the comments are listed, with the content related to the last comment. In order for it to show the stored value related to each comment, this line must be within your child theme’s twentyten_comment() function on functions.php. Right where you have it commented out.

    Why was this commented out version not working for you?

    And now the add_bcw_fields() function. There is no need to place it on comments.php, because it runs as an action callback. Callbacks run at a particular time in the page request process no matter where the code appears in the file. But you certainly cannot have it in more than one place because it throws an error as you have seen.

    Since where it is on functions.php places the input field below the reply box in every case, why would you want to put it on comment.php? Where else would you want it to appear?

    I am going on the assumption that this added field is presented to all logged in commenters as part of the comment/reply form. Whatever is input is related to that one comment. Any value entered for another comment will be related only to that other comment.

    Thus, when the comments are listed, the value stored is to be displayed along with it’s related comment. Once this value and comment are submitted, there is no possibility of editing this field or the comment, except by admins in the backend.

    Is there anything in my assumption that is not correct?

    Thread Starter Taro

    (@sicktb)

    “Is there anything in my assumption that is not correct?”
    I think its the following:

    “Why was this commented out version not working for you?”

    It works good for me…But i still need 6 diffrent pages. Atm i can add fields to the standaard comment that will be the same for every page that you will make on the website. So i thougt i needed to remove the <?php echo get_comment_meta( $comment->comment_ID, ‘bcw_test’, true ); ?> from the function.php. But i think this can be in the function.php but i still need to change the input fields display. See below.

    But in my case i need to have like 6 diffrent comment forms.

    Like the following

    Page1
    Do you like cheese?:
    /////input field/////

    Do you like candy?:
    /////input field/////

    (Submit button)

    Page2
    Do you like flowers?:
    /////input field/////

    Do you like cats or dogs?:
    /////input field/////

    (Submit button)

    Moderator bcworkz

    (@bcworkz)

    You have a design choice to make here. The default comments.php uses calls to wp_list_comments() and comment_form(). If you still make these calls, you can change comment.php all you want and not get the desired results. My proposals focused on altering these calls, but such changes apply to all calls.

    At least wp_list_comments() takes arguments so it can generate different content based on which call back is specified. You will then need 6 different versions of ‘twentyten_comment()’ (with different names) to go with your 6 different comment templates.

    Or you could simply put the loop code and content directly on comments.php and not make a call to wp_list_comments() at all. comment_form() has no such argument possibility. You really do need to put the form code directly on comments.php.

    There are alternatives. You can leave comments.php as it is and make twentyten_comment() and the added form fields responsive to some condition that decides what content to present. For instance, say the content required is based on some custom capability, you could do something like this:

    if( current_user_can('like_cheese')) echo 'Favorite cheese? <input type="text" name="test" id="test" size="22" />'
    else if( current_user_can('like_flowers')) echo 'Favorite flower? <input type="text" name="test" id="test" size="22" />'
    else echo 'What do you like? <input type="text" name="test" id="test" size="22" />'

    and something similar for displaying the input values with each comment. Of course it does not have to be capabilities, it could be based on a user meta value, or the label could even be the user meta, ssuch that every user has their own custom label. There really is no need for 6 different templates, though you can do that as well.

    Or you could dynamically include a code page that only has the content that’s different for whatever capability or condition you’re altering the labels for, a sort of sub-template. All common code remains on comments.php.

    There’s likely other possibilities that aren’t occurring to me. The main thing is where does the code decide to do something different? It should be as late as possible so as much common code as possible is all in one place and what varies is kept to a minimum. You don’t want to find you need to change the same code some time in the future 6 times when once would have sufficed. Put aside your pre-conception of multiple comment pages and look at the problem creatively. There’s possibly a good approach that’s not obvious.

    Thread Starter Taro

    (@sicktb)

    If i’m correct i can use the code on the following way in the comment.php.

    https://pastebin.com/raw.php?i=ZiqrfCz2

    Why do i want templates?: I can easly copy the 6 pages with the correct template selected on it in WordPress with a plugin.

    And im not sure how you whant me to change it to a dynamically include page…(<?php include ‘page1code.php’; ?>)? And put the same code there ?

    Moderator bcworkz

    (@bcworkz)

    Yes, your latest code snippet can work if the input field is showing up where you want it. (and you are really controlling output by capability. I also noticed I forgot a terminating ; after each line)

    If you feel having different templates will work best for you in terms of maintaining your site, then by all means do it that way. I tend to look for the most efficient approach even when doing so would make little difference. Having nearly identical code in multiple places simply violates my sense of order. But if multiple templates work for you and you understand how it all works and you have no issue maintaining such an arrangement, then that is what you should do.

    In that case, abandon the calls to wp_list_comments() and comment_form() and twentyten_comment() and my proposed hooks. Place the related function code directly on each template, altering fields and labels as needed.

    The dynamic include page was just an idea, the include statements would need to be placed in a conditional structure much like the echo input fields were in my last example. Something like this:

    <?php if( current_user_can('like_cheese')) include 'page1code.php';
    else if( current_user_can('like_flowers')) include 'page2code.php';
    else include 'pagedefaultcode.php'; ?>

    It’s the same logic you would be using to decide which template to load. Instead of choosing between 6 full templates, you choose between only the portions that actually change. Depending on what that is, some reorganization may be required. This code would go on the comments.php page where ever the included code would normally be inserted if all code were in one big file.

    I’ve given you a lot of different ideas, I hope it’s not too confusing. Many of these ideas only work for a certain approach. You should now decide which approach you wish to use. I will then stop with the ideas and focus on helping you get your chosen approach working.

    Thread Starter Taro

    (@sicktb)

    I Think multiple templates is the best way for me. It can easily be changed bij admin per page in the wordpress back.

    Im still using your bcw_ so i changed the following code:

    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" />';

    But when i put this to the comment.php it gives me a total of 3 fields now. One is the standaard comment_input. One is the get_comment_meta located in the function. And the last is this new comment input “What do you like?”. When i put something in all the 3 fields it gives me only the result from the normal comment_input and the bcw_test located in function.php.

    I need to remove the one from function and the standaard comment box. To remove the one from function i need to remove this code?:

    // Add extra field to comment form
    function add_bcw_fields(){
    echo '<input type="text" name="test" id="test" size="22" />';
    }
    add_action( 'comment_form_logged_in_after', 'add_bcw_fields' );

    But how to disable the normal comment?

    Moderator bcworkz

    (@bcworkz)

    You will probably throw up your arms is disgust with my next proposal and scream “What a waste of time!”

    We need to start over from the beginning ?? well, sort of.
    It’s entirely my fault. First I was trying to work with the WP way of handling comments, then I was on a mission to make your code efficient, all the while probably confusing you to no end. All I can do now is offer a sincere apology. I feel badly, I can only hope that you at least learned something? I don’t blame you if you do not want to deal with me any more.

    If you’re still with me, you are a saint with your boundless patience. We do not need the add_bcw_fields() function. We still need the bcw_handle_comment($id) function on functions.php. We will need to move the modified twentyten_comments() function. Start with an unmodified copy of the twentyten comments.php file. We will make a custom comments template that contains all the boilerplate which you can then copy 6 times and alter the specifics for each version.

    Insert the modified twentyten_comments() function definition either at the top under the template header or at the bottom after everything, which ever fits your coding style. Be sure it’s contained in a <?php ?> block. Give it a different function name. Also place the new name in the line wp_list_comments( array( 'callback' => 'twentyten_comment' ) );

    Replace the comment_form() line near the bottom with the following distilled down version of the function definition (untested):

    <?php if ( comments_open( $post_id ) ) : ?>
    	<div id="respond" class="comment-respond">
    		<h3 id="reply-title" class="comment-reply-title"><?php comment_form_title( $args['title_reply'], $args['title_reply_to'] ); ?> <small><?php cancel_comment_reply_link( $args['cancel_reply_link'] ); ?></small></h3>
    		<?php if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) : ?>
    			<?php echo $args['must_log_in']; ?>
    		<?php else : ?>
    			<form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>" class="comment-form"<?php echo $html5 ? ' novalidate' : ''; ?>>
    				<?php echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity ); ?>
    				<input type="text" name="test" id="test" size="22" />
    				<?php echo apply_filters( 'comment_form_field_comment', $args['comment_field'] ); ?>
    				<p class="form-submit">
    					<input name="submit" type="submit" id="<?php echo esc_attr( $args['id_submit'] ); ?>" value="<?php echo esc_attr( $args['label_submit'] ); ?>" />
    					<?php comment_id_fields( $post_id ); ?>
    				</p>
    			</form>
    		<?php endif; ?>
    	</div><!-- #respond -->
    <?php else :
    	do_action( 'comment_form_comments_closed' );
    endif; ?>

    Test what you have so far, if all good, expand the test input field to the fields you actually need. The three places to alter the code for the actual fields is the above form code, the modified twentyten_comments() output function, and the bcw_handle_comment($id) save values function. Be sure the field names are coordinated between the three locations. Test again. Tweak the HTML and CSS until it is exactly what you want.

    Once you are happy with the boilerplate, you can copy it multiple times and alter the content for each as needed, and implement the template switching code. While there will be bugs to iron out, this should get you very close to what you want.

    Thread Starter Taro

    (@sicktb)

    “I don’t blame you if you do not want to deal with me any more.”

    Dont worry man. I’m really happy that your helping me at all. Ill gonna have the code above a try. With all the information that you give me i have been looking for some alts myself. And i like to have your opinion about the plugins. And if i can use this. Cus i maybe still want the option to edit the comments in the back of wordpress.

    1) custom-metadata
    2) easy-custom-fields
    3) Custom-Metaboxes-and-Fields-for-WordPress

    I have been looking at this plugins and tryed some. But its all magic for me on how to get it to work. And i mis the information to know if this can be used in my case.

    Moderator bcworkz

    (@bcworkz)

    I’m glad you’re still happy, thanks for hanging in there.

    I’ve no experience with any of those, but on a cursory look, here’s my thoughts.

    Custom-Metadata
    I’m unsure if it can handle 6 different variations of comment forms.

    Easy-Custom-Fields
    This does not appear to support comments, only post types.

    Custom-Metaboxes-and-Fields-for-WordPress
    This does not appear to support comments, only post types. The comment page does not natively support metaboxes. Metaboxes are not necessarily related to metadata, they allow arbitrary HTML to be contained in a draggable container. This is not a plugin, it is theme extension. A bit unorthodox, but it does offer some interesting features, but not for comments.

    So custom-metadata is the only viable candidate. I haven’t installed it, but it’s not clear how it might handle 6 different comment forms.

    I just had another thought, please confirm. I’m assuming (gotta stop doing that) when a user likes cheese, all the comments on the page he is viewing is about cheese, there are no comments about flowers, and so on for each kind of “like”. If the comment page needs to display various likes, not just cheese likes, things get more complicated and the entire scheme may need adjustment.

    Thread Starter Taro

    (@sicktb)

    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///

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