Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to find out gender of commenter and based on that, assign a display picture of commenter who don't have any picture, based on their gender, i.e if a male commenter has no picture, it should assign it a male avatar. I have. I am unable to find ways to query gender.How can i make something like that? And where should i hook it in?

share|improve this question
    
WP doesn't have a field for gender by default, have you set one up yourself? –  ialocin Dec 7 '13 at 16:34
    
@ialocin i can but it won't serve my purpose. I want to set pictures of people if they don't have any, based on gender. I checked gravatar too, it doesn't have field for gender. any workaround for it? As my commenters are also not registered to site, but can comment. –  Nofel Dec 7 '13 at 16:43
2  
Without the information this won't work...ever. Only thing you could do is to add an field to the comments, where a gender has to be picked. –  ialocin Dec 7 '13 at 16:47
1  
Sorry, how do you imagine to base anything on gender if yiu don't have data on gender? –  s_ha_dum Dec 7 '13 at 19:08
    
@s_ha_dum Thought, users had meta data like gender, but i guess not. But is it possible for me to remove any generic (custom uploaded) gravtars, and use my own. from a specific date? –  Nofel Dec 7 '13 at 21:28

2 Answers 2

As per the comments above, you can't have this functionality directly. So we need to add this functionality as per the steps given below.

  1. Add the gender field in the comment form.
  2. Save the gender as meta data of that comment.
  3. Change the get avatar functionality of the comment to pass the default url as per gender.

Let's add the gender field first

// Add the actions to show gender field on comment form
add_action( 'comment_form_logged_in_after', 'wti_additional_comment_field' );
add_action( 'comment_form_after_fields', 'wti_additional_comment_field' );

function wti_additional_comment_field() {
    echo '<p class="comment-form-gender">'.
            '<label for="gender_male">' . __( 'Gender' ) . '</label>'.
            '<input id="gender_male" name="gender" type="radio" value="male" checked="checked" /> ' . __( 'Male' ) .
            '<input id="gender_female" name="gender" type="radio" value="female" />' . __( 'Female' ) .
        '</p>';
}

Let's save the gender value as comment meta data

// Add the action to save the gender in comment
add_action( 'comment_post', 'wti_save_comment_meta_data' );

function wti_save_comment_meta_data( $comment_id ) {
    $gender = wp_filter_nohtml_kses( $_POST['gender'] );
    add_comment_meta( $comment_id, 'gender', $gender );
}

Modify the get avatar functionality to use default image url

// Add the filter to have custom avatar
add_filter('get_avatar', 'wti_custom_avatar', 10, 5);

function wti_custom_avatar($avatar, $id_or_email, $size, $default, $alt) {
    global $comment;

    if ( is_object ( $comment ) && !empty ( $comment ) ) {
        // Remove to avoid recursion
        remove_filter( 'get_avatar', 'wti_custom_avatar' );

        // Get the comment id and gender for that comment
        $comment_id = get_comment_ID();
        $gender = get_comment_meta( $comment_id, 'gender', true );

        // Assign the image url as per gender
        if ( $gender == 'female' ) {
            $default = 'default_female_avatar_url';
        } else {
            $default = 'default_male_avatar_url';
        }

        // Get the avatar with default url
        $avatar = get_avatar( $comment, $size, $default );

        // Add the filter again
        add_filter( 'get_avatar', 'wti_custom_avatar', 10, 5 );
    }

    return $avatar;
}

Few things to note:
- Gender is set to male as default.
- You need to replace the default_female_avatar_url and default_male_avatar_url values with yours.

share|improve this answer

Install BuddyPress, then create xProfile fields such as name, location, gender and so on. Then simply write a conditional statement to find the member's gender. i.e pseudo code:

if gender == male then do this
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.