-1

I'm trying to unset the Facebook link for users who registered in the past 365 days. For users who have completed more than a year, don't unset.

Here's my code which is not working:

function modify_user_contact_methods( $user_contact ) {

    get_currentuserinfo(); 
        $user_data = get_userdata($user_ID);
        $registered_date = $user_data->user_registered;
        if (strtotime($registered_date) > strtotime('-365 days')){
             // Remove user contact methods
         unset( $user_contact['facebook']    );
        }

    return $user_contact;
}
add_filter( 'user_contactmethods', 'modify_user_contact_methods' );

It shows the field. If I remove the condition, it unsets for every user. So the set/unset is correct, only the 365 condition is wrong, I guess. But it seems correct, what amI doing wrong? Please guide.

0

Firs things first... always, always enable debugging. Secondly, you should accept the $user parameter in your callback - otherwise you are always checking the logged in user, not the user within the current context:

function modify_user_contact_methods( $methods, $user ) {
    if ( ! $user instanceof WP_User ) {
        if ( ! $user = wp_get_current_user() )
            return $methods;
    }

    if ( strtotime( $user->user_registered ) > strtotime( '1 year ago' ) ) {
         unset( $methods['facebook'] );
    }

    return $methods;
}

add_filter( 'user_contactmethods', 'modify_user_contact_methods', 10, 2 );

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.