I have created the following "plugin" following the codex but it doesn't seem to work. It is supposed to add 100 points to user meta everytime he/she posts a new blog post.
Could you please let me know what is wrong with it...
function post_published_add_points( $ID, $post ) {
$author = $post->post_author; /* Post author ID. */
add_user_meta( $author, 'Points', '100');
}
add_action( 'publish_post', 'post_published_add_points', 10, 2 );
Thanks! Dragos
I have created the following "plugin" following the codex but it doesn't seem to work. It is supposed to add 100 points to user meta everytime he/she posts a new blog post.
Could you please let me know what is wrong with it...
function post_published_add_points( $ID, $post ) {
$author = $post->post_author; /* Post author ID. */
add_user_meta( $author, 'Points', '100');
}
add_action( 'publish_post', 'post_published_add_points', 10, 2 );
Thanks! Dragos
Share Improve this question asked Jan 16, 2017 at 17:25 Dragos MicuDragos Micu 2132 silver badges9 bronze badges 2- What is the current outcome? What is expected? – Mateusz Marchel Commented Jan 16, 2017 at 17:51
- I am expecting to add a field inside the database in user_meta called Points = 100. I understand that it doesn't add to the value but creates separate fields which is fine for now. Outcome: nothing is being added to the database. – Dragos Micu Commented Jan 18, 2017 at 12:29
3 Answers
Reset to default 0The above function looks fine, and I tested it just to be sure. It may be that you don't like what you're getting.
It adds user meta to the particular author with key Points
and value 100
, like so:
[Points] => Array
(
[0] => 100
)
On the second publish, it will produce the following:
[Points] => Array
(
[0] => 100
[1] => 100
)
And so on.
If you want any other results, then you'll need to adjust parameters or create a function to process the results.
For example, if you wanted to "add" 100 points arithmetically every time - so the second publish produces the value "200," the third "300," and so on - you'd need to get the user_meta, if any, and then arithmetically add each new value, then update the old value with the sum - not simply programmatically "add" the new value to the array.
Alternatively, you could add whatever values together, or turn them into sub-arrays with additional info (whatever other $post values you wanted to save).
add_user_meta
has 3 parameters: the user id, the meta value and a boolean that indicates if it is unique.
By omitting the third value, you will save different rows in the user_meta table with the same value.
It seems that what you want to do is get the current value, add to it, and save the new value, which would look something like this:
function post_published_add_points( $ID, $post ) {
// get author id
$author = $post->post_author;
// get current meta value (and make sure its unique)
$points = get_user_meta( $author, 'Points', true );
// add to value
$points += 100;
// save the new value (again, make sure its unique)
add_user_meta( $author, 'Points', $points, true);
}
add_action( 'publish_post', 'post_published_add_points', 10, 2 );
Hope that helps!
I'd like to have a similar function on my web. This works fine for me, but as mentioned above, the Points value is still "100". I want to add "100" points arithmetically every time - so the next published post produces value 200 and so on.
Could you help me to modify the function, please?
function post_published_add_points( $ID, $post ) {
// get author id
$author = $post->post_author;
// get current meta value (and make sure its unique)
$points = get_user_meta( $author, 'Points', true );
// add to value
$points += 100;
// save the new value (again, make sure its unique)
add_user_meta( $author, 'Points', $points, true);
}
add_action( 'publish_customposttype', 'post_published_add_points', 10, 2 );