Skip to content

Instantly share code, notes, and snippets.

@twright
Created September 24, 2012 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twright/3773601 to your computer and use it in GitHub Desktop.
Save twright/3773601 to your computer and use it in GitHub Desktop.
Get the value of an ECF field for a comment
<?php
/**
* Get the value of an Extra Comment Field.
* @param string $ref The ref code of the field to retrieve e.g. 'ecf_3'.
* @param integer $comment_id The id of the comment.
* @return string|null The value of the user's response to the field.
* @see ecfdb::get_comment_data()
*/
function ecf_get_field_value( $ref, $comment_id = null ) {
// Make use of the global variable containing the Extra Comment Fields
// database and WordPress comment objects
global $ecfdb, $comment;
// If no comment id has been specified, use the id of the current comment
if (is_null($comment_id)) $comment_id = $comment->ID;
// Retrieve the Extra Comment Fields data from the comment metadata
$comment_data = $ecfdb->get_comment_data($comment_id);
// Retrieve the data for the current field, if found, otherwise null
return isset($comment_data[$ref]) ? $comment_data[$ref] : null;
}
// Get the value of field ecf_3 for the current comment
$a = ecf_get_field_value( 'ecf_3' );
// Get the value of field ecf_3 for comment id 4
$b = ecf_get_field_value( 'ecf_3', 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment