Make Fields Read-Only When Editing Child Entry

Make specific fields read-only only when editing a child entry in a Nested Form field. The fields will be editable when first creating the child entry, but read-only when editing.

Requires Gravity Perks Read Only plugin to be installed and activated.

Instructions

Code

Filename: gpnf-gpro-readonly-fields-on-edit.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
/**
 * Gravity Perks // Nested Forms + Read Only // Make Fields Read-Only When Editing Child Entry
 * https://gravitywiz.com/documentation/gravity-forms-nested-forms/
 *
 * Make specific fields read-only only when editing a child entry in a Nested Form field.
 * The fields will be editable when first creating the child entry, but read-only when editing.
 *
 * Instruction Video: https://www.loom.com/share/90fa28244ac54c7991a94a30b545ba63
 *
 * Requires Gravity Perks Read Only plugin to be installed and activated.
 */
// Update "123" to your child form ID and the array to your target field IDs.
add_filter( 'gform_pre_render_123', function( $form ) {

	if ( ! isset( $GLOBALS['gpnf_current_edit_entry'] ) ) {
		return $form;
	}

	// TODO: Field IDs that should be read-only when editing (add/remove field IDs as needed)
	$target_field_ids = array( 1, 2, 3 );

	// Find the target fields and make them read-only
	foreach ( $form['fields'] as &$field ) {
		if ( in_array( $field->id, $target_field_ids ) ) {
			$field->gwreadonly_enable = true;
		}
	}

	return $form;
} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.