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.
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():Edit: Try also:
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.