• Resolved GennaroDiFiandra

    (@gennarodifiandra)


    Good morning everyone. I have a text field with the follow sanitization_cb for the post post type.

    function validateWordField($value, $field_args, $field) {
      $words = explode(' ', trim($value));
    
      if (count($words) > 0) {
        $sanitized_value = sanitize_text_field($words[0]);
      } else {
        $sanitized_value = sanitize_text_field($value);
      }
    
      return $sanitized_value;
    }

    If I digit in the field “abc xyz” and than save the post, the sanitization callback remove ” xyz” but in the field appears “abc xyz” yet.

    If I reload the page (right click and Reload Page), in the field appears “abc xyz” again.

    If I go away from the post screen and then I come back, now the value is “abc”.

    There is a way to show the sanitized value immediately after the post save?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Can you provide your CMB2 configuration for this so that we can try it out on our own with matching settings/fields/etc?

    Thread Starter GennaroDiFiandra

    (@gennarodifiandra)

    Hi Michael, thank you for reply me.

    I have the code on GitHub:

    https://github.com/GennaroDiFiandra/performize-addons/tree/main/esercitazione001

    Here is the code that you ask for:

    function setupWordMetabox() {
      $cmb = new_cmb2_box([
        'id'            =>  'word',
        'title'         =>  __('Word', 'esercitazione001'),
        'object_types'  =>  ['post'],
        'context'       =>  'side',
      ]);
    
      $cmb->add_field([
        'id'               =>  'word',
        'name'             =>  __('Word', 'esercitazione001'),
        'desc'             =>  __('Note: only the first characters sequence without space will be saved', 'esercitazione001'),
        'type'             =>  'text',
        'show_names'       =>  false,
        'sanitization_cb'  =>  '\ESERCITAZIONE001\FIELDS\validateWordField',
      ]);
    }
    
    function validateWordField($value, $field_args, $field) {
      $words = explode(' ', trim($value));
    
      if (count($words) > 0) {
        $sanitized_value = sanitize_text_field($words[0]);
      } else {
        $sanitized_value = sanitize_text_field($value);
      }
    
      return $sanitized_value;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Text field value doesn’t update immediately based on sanitize_cb’ is closed to new replies.