0

I have a scenario where I need to retrieve a custom field ('current_module') from a comment author's user profile and add it to a custom field ('author_current_module') associated with any comment they submit. When I include the following code in my theme's functions.php, nothing is saved to the comment's custom field (I've verified by writing out the variables up until $comment_author_module_id to my log, and everything is working properly).

NOTE: I've set up the custom fields (for both users and comments) using the PODs plugin. This allows me to create a relationship between my user custom field and a custom taxonomy I use throughout the site. As a result, when I retrieve 'current_module' from the user, an array is returned. That's why I'm referencing 'term_taxononmy_id' for the value that is passed to the comment custom field, 'author_current_module'. Again, I've verified this is working up until this point by writing values to my log. I know that $current_author_module_id does indeed contain the term id number that I need to make the rest of my code work. The only thing that seems to be failing is the final add_comment_meta function.

add_filter ('comment_post', 'comment_add_module', 10, 1);

  function comment_add_module($comment_id){
    $comment_data = get_comment($comment_id);
    $comment_author_id = $comment_data->user_id;
    $comment_author_module = get_user_meta($comment_author_id, 'current_module', true);
    $comment_author_module_id = $comment_author_module['term_taxonomy_id'];
    add_comment_meta($comment_id, 'author_current_module', $comment_author_module_id, true);
  }

0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.