[wp-hackers] adding custom fields to a comment form

Gregory Lancaster greglancaster71 at gmail.com
Mon Oct 7 20:31:44 UTC 2013


I am not sure.  I used the code you suggested in functions.php, and then I
edited the comments.php template so it checks the post type - see the
bolded portion.  The field data was showing before, but now it is not.

<?php
/**
 * Use Bootstrap's media object for listing comments
 *
 * @link http://twitter.github.com/bootstrap/components.html#media
 */
class Roots_Walker_Comment extends Walker_Comment {
  function start_lvl(&$output, $depth = 0, $args = array()) {
    $GLOBALS['comment_depth'] = $depth + 1; ?>
    <ul <?php comment_class('media unstyled comment-' . get_comment_ID());
?>>
    <?php
  }

  function end_lvl(&$output, $depth = 0, $args = array()) {
    $GLOBALS['comment_depth'] = $depth + 1;
    echo '</ul>';
  }

  function start_el(&$output, $comment, $depth = 0, $args = array(), $id =
0) {
    $depth++;
    $GLOBALS['comment_depth'] = $depth;
    $GLOBALS['comment'] = $comment;

    if (!empty($args['callback'])) {
      call_user_func($args['callback'], $comment, $args, $depth);
      return;
    }

    extract($args, EXTR_SKIP); ?>


  <li id="comment-<?php comment_ID(); ?>" <?php comment_class('media
comment-' . get_comment_ID()); ?>>
 *   <?php if (get_post_type() == 'profile') { *
*    include(locate_template('templates/comment-profile.php')); } else {*
*    include(locate_template('templates/comment.php')); } ?>*
    }
  <?php
  }

  function end_el(&$output, $comment, $depth = 0, $args = array()) {
    if (!empty($args['end-callback'])) {
      call_user_func($args['end-callback'], $comment, $args, $depth);
      return;
    }
    echo "</div></li>\n";
  }
}

function roots_get_avatar($avatar) {
  $avatar = str_replace("class='avatar", "class='avatar pull-left
media-object", $avatar);
  return $avatar;
}
add_filter('get_avatar', 'roots_get_avatar');




On Mon, Oct 7, 2013 at 12:59 PM, J.D. Grimes <jdg at codesymphony.co> wrote:

> Yes. That means it must be using its own version of the comment_form()
> function. Maybe it has similar hooks?
>
> On Oct 7, 2013, at 3:53 PM, Gregory Lancaster <greglancaster71 at gmail.com>
> wrote:
>
> > awesome! Thats exactly what I needed.  Apparently with this framework the
> > comments.php isnt affected by the add_comment_fields, I have to add it
> > manually.  Since thats the case, I can skip the function right?
> >
> >
> > On Mon, Oct 7, 2013 at 12:26 PM, J.D. Grimes <jdg at codesymphony.co>
> wrote:
> >
> >> Ah, found an action hook you can use to display the field:
> >> comment_form_logged_in_after
> >>
> >> So:
> >>
> >> function add_comment_fields( $fields ) {
> >>
> >>        ?>
> >>
> >>        <p class="comment-form-age">
> >>                <label for="age"><?php _e( 'Age' ); ?></label>
> >>                <input id="age" name="age" type="text" size="30" />
> >>        </p>
> >>
> >>   <?php
> >> }
> >> add_filter( 'comment_form_logged_in_after', 'add_comment_fields' );
> >>
> >>
> >> That will display to logged in users only. Just echo out the form input.
> >> The saving should work the same as before, I think.
> >>
> >> On Oct 7, 2013, at 3:13 PM, "J.D. Grimes" <jdg at codesymphony.co> wrote:
> >>
> >>> No, there's nothing wrong with that code. Actually, it works, but this:
> >>>> // Default comment form elements are hidden when user is logged in
> >>> So, if you want them to show even when the user is logged in, you will
> >> have to use a different hook. I don't know any off of the top of my
> head...
> >>>
> >>> On Oct 7, 2013, at 3:01 PM, Gregory Lancaster <
> greglancaster71 at gmail.com>
> >> wrote:
> >>>
> >>>> I got it :)
> >>>>
> >>>> I followed the smashing guide, and no fields showed up.  So I tried
> just
> >>>> copy pasting their example code and that didnt work either.   Is there
> >>>> something wrong with this code I used here:
> >>>>
> >>>> function add_comment_fields($fields) {
> >>>>
> >>>>  $fields['age'] = '<p class="comment-form-age"><label for="age">' .
> __(
> >>>> 'Age' ) . '</label>' .
> >>>>      '<input id="age" name="age" type="text" size="30" /></p>';
> >>>>  return $fields;
> >>>>
> >>>> }
> >>>> add_filter('comment_form_default_fields','add_comment_fields');
> >>>>
> >>>> function add_comment_meta_values($comment_id) {
> >>>>
> >>>>  if(isset($_POST['age'])) {
> >>>>      $age = wp_filter_nohtml_kses($_POST['age']);
> >>>>      add_comment_meta($comment_id, 'age', $age, false);
> >>>>  }
> >>>>
> >>>> }
> >>>> add_action ('comment_post', 'add_comment_meta_values', 1);
> >>>>
> >>>>
> >>>> On Mon, Oct 7, 2013 at 11:57 AM, J.D. Grimes <jdg at codesymphony.co>
> >> wrote:
> >>>>
> >>>>> I tried to send a reply to that earlier, but I don't think it went
> >> through
> >>>>> - I just resent it now, let me know if you get it.
> >>>>>
> >>>>> On Oct 7, 2013, at 2:54 PM, Gregory Lancaster <
> >> greglancaster71 at gmail.com>
> >>>>> wrote:
> >>>>>
> >>>>>> I was going to (i am tryign to code the plugin I emailed you about
> >> myself
> >>>>>> today, I have all day :)  )  but the is_post_type was depreciated.
> >> Not
> >>>>>> sure what to use now.
> >>>>>>
> >>>>>>
> >>>>>> On Mon, Oct 7, 2013 at 11:50 AM, Chris McCoy <chris at lod.com> wrote:
> >>>>>>
> >>>>>>> I would check for the post type, if its the one you wish to add to
> do
> >>>>> the
> >>>>>>> process like in the tutorial, so it only shows on the comment form
> >> for
> >>>>> the
> >>>>>>> post type.
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>>> On 10/7/2013, 2:45 PM, "BenderisGreat" <greglancaster71 at gmail.com>
> >>>>> wrote:
> >>>>>>>
> >>>>>>>> I did see that, but I need to add the custom comment fields only
> to
> >> the
> >>>>>>>> custom post type
> >>>>>>>>
> >>>>>>>>
> >>>>>>>>
> >>>>>>>> --
> >>>>>>>> View this message in context:
> >>>>>>>>
> >>>>>>>
> >>>>>
> >>
> http://wordpress-hackers.1065353.n5.nabble.com/adding-custom-fields-to-a-c
> >>>>>>>> omment-form-tp42446p42453.html
> >>>>>>>> Sent from the Wordpress Hackers mailing list archive at
> Nabble.com.
> >>>>>>>> _______________________________________________
> >>>>>>>> wp-hackers mailing list
> >>>>>>>> wp-hackers at lists.automattic.com
> >>>>>>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> >>>>>>>
> >>>>>>>
> >>>>>>> _______________________________________________
> >>>>>>> wp-hackers mailing list
> >>>>>>> wp-hackers at lists.automattic.com
> >>>>>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> >>>>>>>
> >>>>>> _______________________________________________
> >>>>>> wp-hackers mailing list
> >>>>>> wp-hackers at lists.automattic.com
> >>>>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> >>>>>
> >>>>> _______________________________________________
> >>>>> wp-hackers mailing list
> >>>>> wp-hackers at lists.automattic.com
> >>>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> >>>>>
> >>>> _______________________________________________
> >>>> wp-hackers mailing list
> >>>> wp-hackers at lists.automattic.com
> >>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> >>>
> >>> _______________________________________________
> >>> wp-hackers mailing list
> >>> wp-hackers at lists.automattic.com
> >>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> >>
> >> _______________________________________________
> >> wp-hackers mailing list
> >> wp-hackers at lists.automattic.com
> >> http://lists.automattic.com/mailman/listinfo/wp-hackers
> >>
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list