Description
The gravityflow_discussion_item filter allows developers to modify the markup or text of each Discussion field comment before it is displayed. It applies wherever comments are shown, including the inbox, the entry detail page, the Discussion field merge tag and entry exports.
Usage
The following would apply to all forms.
add_filter( 'gravityflow_discussion_item', 'your_function_name', 10, 5 );
Parameters
| Parameter | Type | Description |
|---|---|---|
| $discussion_item | string | The comment markup when the format is html, or the comment text when the format is text. |
| $item | array | The comment data: id, assignee_key (for example user_id|1), timestamp and value. |
| $format | string | The requested format: html or text. |
| $entry_id | integer or null | The ID of the entry when the field is being edited, or null in other locations. |
| $field | Field Object | The Discussion field currently being processed. |
Examples
Style comments from the entry creator differently.
This example adds a creator class to comments posted by the user who submitted the entry and a not-creator class to all other comments.
add_filter( 'gravityflow_discussion_item', 'my_discussion_item_creator_class', 10, 5 );
function my_discussion_item_creator_class( $discussion_item, $item, $format, $entry_id, $field ) {
if ( $format !== 'html' ) {
return $discussion_item;
}
// $entry_id is only set while the field is editable; fall back to the entry being viewed.
$entry_id = $entry_id ? $entry_id : absint( rgget( 'lid' ) );
if ( ! $entry_id ) {
return $discussion_item;
}
$entry = GFAPI::get_entry( $entry_id );
if ( is_wp_error( $entry ) ) {
return $discussion_item;
}
$is_creator = rgar( $item, 'assignee_key' ) === 'user_id|' . rgar( $entry, 'created_by' );
$class = $is_creator ? 'creator' : 'not-creator';
return preg_replace( '/class="gravityflow-discussion-item/', 'class="gravityflow-discussion-item ' . $class, $discussion_item, 1 );
}
Then add CSS like the following to your theme to align and color the comments.
.gravityflow-discussion-item.creator {
margin-right: 20%;
padding: 8px 12px;
background-color: #eef5fc;
}
.gravityflow-discussion-item.not-creator {
margin-left: 20%;
padding: 8px 12px;
background-color: #f3f3f3;
}
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Source Code
This filter is located in includes/fields/class-field-discussion.php.
Since
This filter was added in Gravity Flow 3.0.0