Using WordPress ‘add_meta_boxes_comment’ PHP action

The add_meta_boxes_comment action hook is a WordPress PHP function that fires when comment-specific meta boxes are added. It is typically used to add custom meta boxes to the comments edit screen.

Usage

To use the add_meta_boxes_comment action hook, you can add your custom code in the following way:

add_action( 'add_meta_boxes_comment', 'your_function_name' );

function your_function_name( $comment ) {
	// your custom code here
	return $comment;
}

In the above example, your_function_name is the name of your custom function that will be executed when the add_meta_boxes_comment action hook is fired. You can replace it with any other name you like.

Parameters

The add_meta_boxes_comment action hook has one parameter:

  • $comment (WP_Comment): The comment object.

More information

For more information on how to use the add_meta_boxes_comment action hook, you can refer to the WordPress Developer Resources here.

Examples

Here are some practical examples of how to use the add_meta_boxes_comment action hook:

Add custom meta box to the comments edit screen

add_action( 'add_meta_boxes_comment', 'your_function_name' );

function your_function_name( $comment ) {
	add_meta_box(
		'my_custom_meta_box',
		__( 'My Custom Meta Box', 'textdomain' ),
		'your_callback_function',
		'comment',
		'normal',
		'high'
	);
}

In the above example, a custom meta box with the ID my_custom_meta_box and the title My Custom Meta Box is added to the comments edit screen using the add_meta_box() function.

Add custom fields to the comments edit screen

add_action( 'add_meta_boxes_comment', 'your_function_name' );

function your_function_name( $comment ) {
	add_comment_meta( $comment->comment_ID, 'my_custom_field', 'custom_value' );
}

In the above example, a custom field with the key my_custom_field and the value custom_value is added to the comment using the add_comment_meta() function.

Modify existing meta boxes on the comments edit screen

add_action( 'add_meta_boxes_comment', 'your_function_name' );

function your_function_name( $comment ) {
	global $wp_meta_boxes;

	if ( isset( $wp_meta_boxes['comment']['normal']['core']['commentstatusdiv'] ) ) {
		$wp_meta_boxes['comment']['normal']['core']['commentstatusdiv']['title'] = __( 'Custom Title', 'textdomain' );
	}
}

In the above example, the title of an existing meta box with the ID commentstatusdiv is modified using the $wp_meta_boxes global variable.

Remove existing meta boxes from the comments edit screen

add_action( 'add_meta_boxes_comment', 'your_function_name' );

function your_function_name( $comment ) {
	remove_meta_box( 'commentstatusdiv', 'comment', 'normal' );
}

In the above example, an existing meta box with the ID commentstatusdiv is removed from the comments edit screen using the remove_meta_box() function.

Add custom CSS styles to the comments edit screen

add_action( 'admin_enqueue_scripts', 'your_function_name' );

function your_function_name() {
	$screen = get_current_screen();

	if ($screen->id === 'edit-comments' ) {
wp_enqueue_style( 'custom_styles', plugin_dir_url( FILE ) . 'custom-styles.css' );
}
}

In the above example, custom CSS styles are added to the comments edit screen using the `wp_enqueue_style()` function. The styles are only added if the current screen is the comments edit screen.