pbearne / wp-author-avatars

copy of the SVN at http://wordpress.org/extend/plugins/author-avatars/ wordpress plugin
4 stars 7 forks source link

Adding a custom user field eg location #16

Closed veromary closed 5 years ago

veromary commented 5 years ago

I can see the code for adding custom user fields commented out towards the end of author-avatars.php

I'd like to use the User's Location in the list. Just a text field.

If I bodge this together myself, then next thing to figure out is how to prevent wordpress automatically over-writing the plugin on update, while hopefully having a way - maybe via git - to update the plugin.

veromary commented 5 years ago

Could I add in the code which would print the location if it was in the user meta?

Or I just have to remember not to update the plugin.

veromary commented 5 years ago

Then I saw the filter hooks, and thought I'd have a go using those. Unfortunately, though the get meta function works as planned within User....php, outside it doesn't take the userid into consideration, so I get all contributors listed with the location of whichever contributor is responsible for the current page.


function aa_print_location( $args ) {
    $search = array('<a href="mailto:','">%1$s</a>');
    $replace = "";
    $useremail = str_replace( $search, $replace, $args );
    $aauser = get_user_by( 'user_email', $useremail );
    $location = get_the_author_meta( 'location', $aauser->user_id );
    return $location;
}

add_filter('aa_user_email_url_template', 'aa_print_location');
pbearne commented 5 years ago

Hi Veromary

I think you are using the wrong filter

apply_filters( 'aa_user_final_content', $html, $user )

this has all the HTML of user block

you should be able to find the user location and to a str_replace to add where you need it

try this code NOT TESTED

`function aa_add_location( $html, $user) {

$location = get_the_author_meta( 'location', $user->user_id );
$useremail = str_replace( '<a href="mailto', $location  . '<a href="mailto', $html );

return $html;

}

add_filter('aa_user_email_url_template', 'aa_add_location', 10, 2);`

Paul

veromary commented 5 years ago

Thanks - here's the final answer - I didn't need to include the email address. It was that "10, 2" bit that I missed before. It's the get_author_meta function trying to be clever, so ended up using get_user_meta instead. Just hope the client doesn't want email addresses listed :)


function aa_print_location( $html, $useremail, $user ) {

    $location = get_user_meta( $user->user_id, 'location', true );
    return $location;
}

add_filter('aa_user_email_url_template', 'aa_print_location', 10, 3);