0

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

2
  • What is the current outcome? What is expected? 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. Jan 18, 2017 at 12:29

3 Answers 3

0

The 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).

0

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!

0

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 );

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.