Order of Operation for these three hooks

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

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.

Leave a Comment