• Resolved mburtis

    (@mburtis)


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

    I’ve set up the custom fields (for both users and comments) using PODs plugin. I suspect that something is happening within PODS to intervene in the add_comment_meta function. Strangely, when I return the output of the function to my log file, I’m getting the ID of the comment instead of the ID of the custom field.

    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);
      }
    
Viewing 1 replies (of 1 total)
  • Plugin Support Paul Clark

    (@pdclark)

    Hi @mburtis,

    This variation of your code worked for me:

    <?php
    
    add_filter ( 'comment_post', 'comment_add_module', 20, 3 );
    
    function comment_add_module( $comment_id, $comment_approved, $comment_data ){
    	$comment_author_module = get_user_meta( $comment_data['user_id'], 'current_module', true );
    
    	add_comment_meta(
    		(int) $comment_id,
    		'author_current_module',
    		(int) $comment_author_module['term_id'],
    		true
    	);
    }

    Where author_current_module is a Number field and current_module is a relationship from Users to a custom taxonomy.

    Some changes to note:

    – Hook priority changed from 10 to 20. This was likely the core issue.
    – Number of hook arguments changed from 1 to 3, according to https://developer.www.ads-software.com/reference/hooks/comment_post/
    – Saved term_id rather than term_taxonomy_id.

Viewing 1 replies (of 1 total)
  • The topic ‘Problem with Updating Comment Meta’ is closed to new replies.