Using WordPress ‘get_commentdata()’ PHP function

The get_commentdata() WordPress PHP function retrieves an array of comment data about a specific comment using its comment ID.

Usage

get_commentdata($comment_id, $no_cache = false, $include_unapproved = false);

Input:

  • $comment_id (int): The ID of the comment
  • $no_cache (int): Whether to use the cache (cast to bool), default is false
  • $include_unapproved (bool): Whether to include unapproved comments, default is false

Parameters

  • $comment_id (int): The ID of the comment
  • $no_cache (int): Whether to use the cache (cast to bool)
  • $include_unapproved (bool, optional): Whether to include unapproved comments, default is false

More information

See WordPress Developer Resources: get_commentdata

Examples

Get comment data by comment ID

This example retrieves the comment data for a comment with the ID of 15.

// Fetch comment data
$comment_data = get_commentdata(15);

// Print comment data
print_r($comment_data);

Get comment data without caching

This example retrieves comment data without using cache.

// Fetch comment data without caching
$comment_data = get_commentdata(20, 1);

// Print comment data
print_r($comment_data);

Get comment data including unapproved comments

This example retrieves comment data including unapproved comments.

// Fetch comment data including unapproved comments
$comment_data = get_commentdata(25, 0, true);

// Print comment data
print_r($comment_data);

Display specific comment data

This example retrieves comment data and displays the comment author’s name and the comment content.

// Fetch comment data
$comment_data = get_commentdata(30);

// Print comment author and content
echo 'Author: ' . $comment_data['comment_author'] . '<br>';
echo 'Comment: ' . $comment_data['comment_content'];

Check if a comment is approved

This example checks if a comment is approved and displays a message accordingly.

// Fetch comment data
$comment_data = get_commentdata(35);

// Check if the comment is approved
if ($comment_data['comment_approved'] == 1) {
    echo 'This comment is approved.';
} else {
    echo 'This comment is not approved.';
}