0

I am trying to find a way of getting a users membership account age?

For example - I want to display:

Member since - 2010

I have looked here:

https://codex.wordpress.org/Function_Reference and all over the web but I can not find anything.

Can you help please?

  • 1
    Could you add the code you used to get result/output? Because, sorry to say, you didnt do a real search OR used the wrong text.... Take a look here or here or here and so on. – Charles Oct 6 '16 at 13:50
  • Hi Charles, I have already ready these three links and I am sorry to say this didn't work for me. Thanks! – user3502952 Oct 6 '16 at 15:26
7

The answer from @Ralf912 (the second 'here' in my comment!) is all you need/want.

This little function should help you out:
(please make a backup of functions.php before adding this code snippet in that file.

/**
 * Display Members registration year
 *
 * @author @Ralf912
 * @return date(year)
 * @uses   echo do_shortcode( '[memberSince]' );
 *
 * @version Wordpress 4.6.1
 */
add_shortcode( 'memberSince', 'wpse241751_display_year' );
function wpse241751_display_year(){
    $users = get_users();

    foreach( $users as $user ) {
        $udata = get_userdata( $user->ID );
        $registered = $udata->user_registered;
        printf( 'Member since %s<br>', date( 'Y', strtotime( $registered ) ) );
    }
}

You can use now the following shortcode [memberSince] for explample in a template you can add: echo do_shortcode( '[memberSince]' ); this will display(example): Member since 2016

To add also a month you could change in that function date( 'Y', into date( 'M Y',.

Please read also Codex info about: get_userdata and get_users


Last edit/update

There is also a way to display the outcome by adding the code directly in a Template by using the following snippet.

 $users = get_users();
 foreach( $users as $user )
 {
    $udata = get_userdata( $user->ID );
    $registered = $udata->user_registered;
    printf( 'Member since %s<br>', date( 'Y', strtotime( $registered ) ) );
}

Be aware that you have to remove the shortcode from the template and that when using this approach the function is useless and therefore also should be removed from functions.php.

  • Hey Charles, thanks for much for this but it is printing out several times - see my screenshot: – user3502952 Oct 6 '16 at 17:53
  • postimg.org/image/k8kflu5kr – user3502952 Oct 6 '16 at 17:53
  • To be honest, I have no clue, and it could maybe help to add that part where it comes from, a template? Imho it shouldn't repeat itself for any reason. – Charles Oct 6 '16 at 22:33
  • Wicked no worries. Thank you very much for your help I will accept your solution. Have a great weekend :) – user3502952 Oct 7 '16 at 5:38
  • @Ben, thank you fro responding, I will update/edit the answer with anothor approach which (maybe)prevents the "echoing" from output on the screen. Meaning by that, code which you can use directly in a Template. Have a great weekend too. – Charles Oct 7 '16 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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