2

What is the order of firing for these three hooks?

    add_filter( 'comments_array', array( $this, 'BuildCommentsArray' ), 10, 2 );
    add_action( 'preprocess_comment', array( $this, 'write' ), 10, 1 );
    add_action( 'comment_post', array( $this, 'add_meta' ), 10, 1 );

This is what I'm trying to achieve, but I'm getting a loop.

In BuildCommentsArray, posts from a forum are found that have not been written to WordPress. Write the post to a WordPress comment and write to commentmeta.

 update_comment_meta( $comment_id, 'origin', 'XenForo');

The preprocess_comment takes a new WordPress comment and writes it to the forum. There is a conditional to look for the 'origin' meta value. If not present then write to the forum.

The comment_post adds meta to the WP comment with post_id and thread_id information.

What's happening is

  1. the forum post is written to WordPress database.
  2. the 'origin' meta value is in the WordPress commentmeta

However, a new forum post is created from the WordPress comment despite having the 'origin' conditional.

$origin = get_comment_meta( $comment->comment_ID, 'origin', true );
if ( $origin != 'XenForo') {
    // Write to Forum
}

This brings me to the question, is the preprocess_comment hook firing before the comments_array?

In which case, can I influence the firing by changing the 10 in the add_action?

Thank you for pointing to any information to help.

1 Answer 1

2

The comments_array filter as part of the comments_template() function, the purpose of which is to load a comment template. It filters comments being prepared to be rendered in the comment section of singular type pages.

The preprocess_comment and comment_post actions (in that order) both fire as part of the wp_new_comment() function, the purpose of which is to process/sanitize a new comment before sending it into the database.

As such, comments_array, on one hand, preprocess_comment and comment_post on the other, are completely separate processes: one is retrieving data, the other is saving data. Depending on which direction the data is flowing, either the filter will fire, or the actions will fire, but they will never all fire together! Or, rather, the actions will fire when a new comment is saved, and then when the page reloads with the new comments saved, the filter will fire on the reload. But which comes first, the chicken or the egg, will depend on which point in the process you enter it.

Furthermore, you can't influence which functions run on different hooks by changing the priorities. The priority only prioritizes functions that are attached to the same hook.

2
  • Thank you for the very clear explanation. My calling the file with a wp_new_comment() was writing the forum post to WP comments then calling the preprocess_comment which was writing to the forum. This is why there was a loop. Can you edit how you figured this out? I spent weeks with debug bar and add-ons trying to understand. Is there a better way? I'm marking this accepted. And thank you for the tag edits. And do you think I should use wp_insert_comment instead of wp_new_comment ?
    – LPH
    Commented Apr 21, 2016 at 22:38
  • Ah, the beauty of open source is you can read the core code! The relevant core files are wp-includes/comment.php and wp-includes/comment-template.php. I'll leave it to another question to resolve the difference between wp_insert_comment() vs wp_new_comment(), but I'll bet that if you look at the two functions in core's comment.php you'll figure it out quite easily. Cheers!
    – Caspar
    Commented Apr 21, 2016 at 23:16

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.