1

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.

2 Answers 2

3

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' );
4
  • 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 Commented Jun 5, 2013 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? Commented Jun 5, 2013 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 Commented Jun 8, 2013 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. Commented Jun 8, 2013 at 16:54
0

Just to add to this thread - it may help someone. In our case we were referencing functions from one plugging in another.

The issue was the load order of plugins. One plugin was loaded with 'plugins_loaded' the other with 'init'. The solution was to properly load and prioritise the load order using the same hook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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