• 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 5 replies - 76 through 80 (of 80 total)
  • Moderator bcworkz

    (@bcworkz)

    The main change is the non-admin users can delete their posts from the front end without any special capability required. Perhaps less important if the user can edit their comment, but we are not there yet. I would encourage you to use my latest version, but of course you are free to do as you please.

    I think front end editing is not too complex for a few fields, but when there are many fields and 6 versions of the many fields, it will get messy, but the underlying logic is still not too complex. The main issue is any plugin that allows front end comment editing will only work for the timestamp, not any added custom fields.

    This is the problem also with the edit button. As you suspected, the back end edit button only allows editing the timestamp, the added custom fields do not appear.

    As for preventing users from editing their profile, it appears making it securely impossible to do this is not that easy. Being able to edit one’s own password and personal data is a basic capability in WP, meaning that capability is not even checked, so it’s not possible to manipulate capabilities with role manager to alter profile access.

    It’s not too hard to make it difficult to edit one’s profile. You could remove the menu item so there is no apparent way to access the profile page. However, if someone were to type in the correct URL in their browser, the profile page would be sent and any revisions happily accepted and committed to the DB. Not secure at all.

    I currently do not see a clean, secure way to block the user from his profile. I’ll investigate some more, but it does not look promising.

    I’m afraid a front end edit comment functionality is more coding than I’m able to help you with, but here is what I would do if I did have the time. Using my delete comment approach as a guide, something similar could be done to handle a form POST. The comment display actually is an edit form, but it is made to appear as normal content using CSS. When the edit button is clicked, the CSS is changed so the content now appears in a form with an update button.

    When the update button is clicked, the form data is posted to the single page’s permalink. The POST is intercepted just as is done for deleting the comment. The comment is updated, and the single page response is sent reflecting the new comment data.

    Thread Starter Taro

    (@sicktb)

    I currently do not see a clean, secure way to block the user from his profile. I’ll investigate some more, but it does not look promising.

    I’ll gonna look what i can do with some redirects. Maybe this will work for users and not for admin.

    I’m afraid a front end edit comment functionality is more coding than I’m able to help you with, but here is what I would do if I did have the time. Using my delete comment approach as a guide, something similar could be done to handle a form POST. The comment display actually is an edit form, but it is made to appear as normal content using CSS. When the edit button is clicked, the CSS is changed so the content now appears in a form with an update button.

    When the update button is clicked, the form data is posted to the single page’s permalink. The POST is intercepted just as is done for deleting the comment. The comment is updated, and the single page response is sent reflecting the new comment data.

    Well i’m not gonna fix this on my own. So i need this help. It would be the best if you could help me… Im not sure if it helps than i’m willing to pay for the option? Just need to know what it will cost me to get it working. Or maybe you know someone on this forum that maybe wants to help me?

    Moderator bcworkz

    (@bcworkz)

    I’ll gonna look what i can do with some redirects.

    That’s a good idea! Maybe any request for the profile page gets sent to a screening page. If the user qualifies, they are forwarded back to the profiles page, otherwise they get an error message and the process dies. The trick will be how to prevent an endless redirect loop in a way that is secure. I’m not sure how to manage that.

    I appreciate your willingness to pay, but you cannot solicit work in this forum. Doing so can result in your topic being closed. Accordingly, I cannot accept any employment offers through this forum.

    I truly regret leaving you hanging like this, but I have to draw a line somewhere. We’ve accomplished your initial need of adding custom comment fields some time ago. We’ve been focused on little bit by bit add on functionality for some time now. I usually only offer advice and insight here and not specific code, but I could tell you really needed help so I made an exception. But then you keep having additional needs, and now I feel like I’ve deceived your client by allowing you to misrepresent your qualifications. Now, there’s certainly nothing wrong with offering a service and hiring a skilled person to do that service on your behalf. It’s not like some major ethical dilemma has cropped up here. I hope you can understand this from my viewpoint.

    I am just doing what I should have done some time ago. I’m sorry if I misled you about how much I’m willing to contribute to your project. I will still continue to happily offer any advice and insight you may need, but I can’t provide any more coding work for you.

    You clearly need help to continue with this project. Here are a couple WordPress specific resources where soliciting coding work is the primary focus:
    https://jobs.wordpress.net/
    https://directory.codepoet.com/

    I wish you the best of luck, I’m sorry it needs to be this way.

    Thread Starter Taro

    (@sicktb)

    Well im realy glad you helped me with the information and the code! I will finetune the website with some coding that i have to do. And i hope to see you at https://jobs.wordpress.net/

    Thread Starter Taro

    (@sicktb)

    Changed to ninja-forms. With a small edit to the code.

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