Title: init hook function causes comments to be closed.
Last modified: August 28, 2021

---

# init hook function causes comments to be closed.

 *  Resolved [delanthear](https://wordpress.org/support/users/delanthear/)
 * (@delanthear)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/init-hook-function-causes-comments-to-be-closed/)
 * Hi
 * I’ve written myself a small plugin which takes a custom post type and uses an
   init hook action to select them and register them as block pattern using register_block_pattern
   and register_block_pattern_category.
 * This all works really well, BUT, it seems to cause a problem when you try and
   submit a comment. Basically, I’m getting a “Sorry, comments are closed for this
   item.” from wp-comments-post.php.
 * If I comment out the init hook call, comments work again.
 * I’m not really sure how to approach debugging this one!
 * This is the hook being called:
 *     ```
       function grab_patterns_for_registration() {
   
       	$patterns = new WP_Query( 
       		array( 
       			'post_type' => 'patterns',
       			'post_status' => 'publish' 
       		) 
       	);
   
       	if ( $patterns->have_posts() ) {
       		while ( $patterns->have_posts() ) {
   
       			$patterns->the_post();	
   
       			$pattern_name = 'patterns/' . get_post_field( 'post_name');
       			$content = get_the_content();
       			$title = get_the_title();
       			$postID = get_the_ID();		
       			$category = get_post_meta(get_the_ID(),'pattern_category', true);			
   
       			/* if no category is set, give it a default */
       			if (!$category) {
       				$category = 'Custom Patterns';
       			}
   
       			/* Add the category */
       			register_block_pattern_category(
       				$category,
       				array( 'label' => __( $category, 'addpatterns' ) )
       			);
   
       			/* Register the block category */
       			register_block_pattern($pattern_name, [
       				'title' => __($title, 'addpatterns'),
       				'categories' => [$category],
       				'content' => $content,
       			]);
       		}
       	} 
       	/* Restore original Post Data */
       	wp_reset_postdata();
       }
       add_action('init', 'grab_patterns_for_registration');
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Thread Starter [delanthear](https://wordpress.org/support/users/delanthear/)
 * (@delanthear)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/init-hook-function-causes-comments-to-be-closed/#post-14814834)
 * Just looking at the code, my money is on me using wp_reset_postdata(); I don’t
   really follow how it works though so…
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/init-hook-function-causes-comments-to-be-closed/#post-14816253)
 * You’re probably right. Try commenting out just the reset line to verify. With
   that line out, your callback will likely mess up regular posts requests. It’s
   just a test, not a fix.
 * The solution may involve conditional execution based on the requested path saved
   in $_SERVER. You won’t need block patterns when a comment is submitted.
 *  Thread Starter [delanthear](https://wordpress.org/support/users/delanthear/)
 * (@delanthear)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/init-hook-function-causes-comments-to-be-closed/#post-14816283)
 * Thanks, I just tried:
 *     ```
       if ($_SERVER['REQUEST_URI'] == "/wp-comments-post.php") {
       	return;
       }
       ```
   
 * and it works. Feels a bit dirty though!
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/init-hook-function-causes-comments-to-be-closed/#post-14819477)
 * 🙂 I agree, but it’ll be fine. Ideally we’d determine the root cause in resetting
   post data and solve it directly. The reset function sets up global post data 
   related to the current post that was in effect before your query. The comments
   post routine queries for the post related to the comment and setting up new post
   data for it through the reset function is clearing some field so that the comment
   defaults to closed status. AFAICT anyway.
 * Even knowing the cause doesn’t point us to a good clean solution. There are no
   handy filters or actions we could use that would let us develop a cleaner solution.
 *  Thread Starter [delanthear](https://wordpress.org/support/users/delanthear/)
 * (@delanthear)
 * [4 years, 9 months ago](https://wordpress.org/support/topic/init-hook-function-causes-comments-to-be-closed/#post-14820374)
 * I’m cool with that then! 😀
 * Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘init hook function causes comments to be closed.’ is closed to new replies.

## Tags

 * [Comment Error](https://wordpress.org/support/topic-tag/comment-error/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 2 participants
 * Last reply from: [delanthear](https://wordpress.org/support/users/delanthear/)
 * Last activity: [4 years, 9 months ago](https://wordpress.org/support/topic/init-hook-function-causes-comments-to-be-closed/#post-14820374)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
