Skip to content

Commit 93f28b8

Browse files
authored
gw-display-html-field-on-entry-detail.php: Added a snippet to display HTML field content on Entry Details page.
1 parent 1aaed7b commit 93f28b8

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Gravity Wiz // Gravity Forms // Display HTML Field on Entry Details
4+
*
5+
* Displays dynamically rendered HTML field content (with merge tags and shortcodes) on the entry detail and entry list view.
6+
*
7+
* Plugin Name: Display HTML Field on Entry Details
8+
* Plugin URI: http://gravitywiz.com/
9+
* Description: Display HTML field content (with Live Merge Tags and shortcodes) dynamically in the entry detail view.
10+
* Author: Gravity Wiz
11+
* Version: 0.1
12+
* Author URI: http://gravitywiz.com
13+
*/
14+
class GW_Display_HTML_Field_Entry_Detail {
15+
16+
private $_args = array();
17+
18+
public function __construct( $args = array() ) {
19+
$this->_args = wp_parse_args(
20+
$args,
21+
array(
22+
'form_id' => false,
23+
'field_id' => false,
24+
)
25+
);
26+
27+
add_action( 'init', array( $this, 'init' ) );
28+
}
29+
30+
public function init() {
31+
add_action( 'gform_entry_detail', array( $this, 'display_html_field_content_entry_detail' ), 10, 2 );
32+
add_filter( 'gform_get_input_value', array( $this, 'display_html_field_content_entry_list' ), 10, 4 );
33+
}
34+
35+
private function is_applicable_form( $form ) {
36+
$form_id = is_array( $form ) && isset( $form['id'] ) ? $form['id'] : (int) $form;
37+
return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id'];
38+
}
39+
40+
private function is_applicable_field( $field ) {
41+
return $field->get_input_type() === 'html' &&
42+
( empty( $this->_args['field_id'] ) || (int) $field->id === (int) $this->_args['field_id'] );
43+
}
44+
45+
private function process_html_content( $content, $form, $entry ) {
46+
// Process Live Merge Tags if available.
47+
if (
48+
method_exists( 'GP_Populate_Anything_Live_Merge_Tags', 'has_live_merge_tag' ) &&
49+
GP_Populate_Anything_Live_Merge_Tags::get_instance()->has_live_merge_tag( $content )
50+
) {
51+
$content = gp_populate_anything()->live_merge_tags->replace_live_merge_tags_static( $content, $form, $entry );
52+
}
53+
54+
// Replace merge tags and shortcodes.
55+
$content = GFCommon::replace_variables( $content, $form, $entry );
56+
$content = do_shortcode( $content );
57+
58+
return ! empty( $content ) ? wp_kses_post( $content ) : '';
59+
}
60+
61+
public function display_html_field_content_entry_detail( $form, $entry ) {
62+
if ( ! $this->is_applicable_form( $form ) ) {
63+
return;
64+
}
65+
66+
foreach ( $form['fields'] as $field ) {
67+
if ( $this->is_applicable_field( $field ) ) {
68+
$content = $this->process_html_content( $field->content, $form, $entry );
69+
70+
if ( $content ) {
71+
printf(
72+
'<h4>%s</h4><div>%s</div><hr>',
73+
esc_html( $field->label ),
74+
$content
75+
);
76+
}
77+
}
78+
}
79+
}
80+
81+
public function display_html_field_content_entry_list( $value, $entry, $field, $input_id ) {
82+
static $is_running = false;
83+
84+
if ( $is_running || rgget( 'page' ) !== 'gf_entries' || ! $this->is_applicable_field( $field ) ) {
85+
return $value;
86+
}
87+
88+
$form = GFAPI::get_form( $field->formId );
89+
if ( ! $this->is_applicable_form( $form ) ) {
90+
return $value;
91+
}
92+
93+
$is_running = true;
94+
$content = $this->process_html_content( $field->content, $form, $entry );
95+
$is_running = false;
96+
97+
return $content ?: $value;
98+
}
99+
}
100+
101+
# Configuration
102+
new GW_Display_HTML_Field_Entry_Detail( array(
103+
'form_id' => 846, // Replace with your form ID or leave false for all.
104+
'field_id' => 4, // Replace with your HTML field ID or leave false to process all HTML fields.
105+
) );

0 commit comments

Comments
 (0)