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 have spent many time looking for a solution to my issue, but in vain. Your help is invaluable.

I am creating a simplified badge system. The goal is to increment the number of badges for a user if he performs some things on the website. For example, for each post published, I give a user one badge.

To do so, I opted for user meta data, and the use of publish_post action hook.

in my functions.php, I put:

function publish_badge($post_ID) {

global $current_user; 
get_currentuserinfo();

$number_of_badges= get_user_meta($current_user->id,'user_number_of_badges',true);

$number_of_badges= $number_of_badges + 1;

update_user_meta($current_user->id, 'user_number_of_badges', $number_of_badges);

return $post_ID;                
}

add_action('publish_post', 'publish_badge');

I am new in using Wordpress action hooks, and I spent a lot of time making it work. I know I am missing something, since for other hooks, the problem remains.

Note I have in total 4 actions for which I want to give users more badges: publish_post,edit_post,wp_login,comment_post.

Your usual help is always appreciated.

Edit:

I found solution for all the hooks except wp_login, In fact, I am using custom post types so I should change my hook names. in other words, publish_post will become publish_cpt.

What to do with wp_login??

    function login_badge($user_login) {
    global $current_user; 

    get_currentuserinfo();
 $number_of_badges= get_user_meta($current_user->id,'user_number_of_badges2',true);
   $number_of_badges= $number_of_badges +5;

  update_user_meta($current_user->id, 'user_number_of_badges2', $number_of_badges);

    }


add_action('wp_login', 'login_badge');

thank you for your time.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You're probably running into a problem with $current_user->id being deprecated since WordPress 2.1. Specifically, I get this notice in the error log:

Notice: WP_User->id was called with an argument that is deprecated since version 2.1! Use WP_User->ID instead. in /xxx/xxx/public_html/wordpress/wp-includes/functions.php on line 2923.

Use $current_user->ID instead. Or you may be able to use get_current_user_id():

add_action( 'wp_login', 'login_badge' );
function login_badge( $user_login ) {
    if ( get_user_meta( get_current_user_id(), 'user_number_of_badges2', true ) )
        update_user_meta( get_current_user_id(), 'user_number_of_badges2', get_user_meta( get_current_user_id(), 'user_number_of_badges2', true ) + 5 );
}

Edit: Try also:

function login_badge( $login ) {

    $user = get_userdatabylogin($login);

    $number_of_badges= get_user_meta($user->ID,'user_number_of_badges2',true);
    $number_of_badges= $number_of_badges + 30;

    update_user_meta($user->ID, 'user_number_of_badges2', $number_of_badges);
}
add_action( 'wp_login', 'login_badge' );
share|improve this answer
    
I thank you very much for your information. Unfotunately, I still cannot find solution for my login hook. I will come back here to provide with the solution when I find it. +1 –  whiteletters and blankspaces Jun 5 '13 at 6:06
    
Both the solution above and changing your code to use $current_user->ID worked fine for me on a test web site. How are you testing for success or failure? –  Charles Clarkson Jun 5 '13 at 10:58
1  
I agree with you, your approach is logical, for me it doesn' work even if I made the change of adding 2 parameters to update_user_meta (get_current_user_id() and 'user_number_of_badges2'). You only put in your solution one parameter( look at my edit). thank you anyway, I also gave the solution for my case above your answer –  whiteletters and blankspaces Jun 8 '13 at 15:36
    
You're right. You have to watch me like a hawk. Thanks for sharing your solution. I'm sure some future reader will appreciate your effort. –  Charles Clarkson Jun 8 '13 at 16:54

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.