Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter noquierouser

    (@noquierouser)

    I finally managed to get it working! This is what I did:

    
    add_action('cmb2_render_test_text_field', 'habsup_render_test_text_field', 10, 5);
    function habsup_render_test_text_field($field, $escaped_value, $object_id, $object_type, $field_type_object) {
      echo $field_type_object->input([
        'type'     => 'text',
        'class'    => 'cmb_text_small',
        'disabled' => 'disabled'
      ]);
    }
    
    add_action('cmb2_sanitize_test_text_field', 'habsup_sanitize_test_text_field', 10, 2);
    function habsup_sanitize_test_text_field($override_value, $value) {
      return str_replace('https://my.url/id/', '', get_post_meta(get_the_ID(), 'my_url', true));
    }
    

    Basically I defined a custom field type which is just a disabled text field, which will contain the subset of the original and editable field.

    I have two fields: my_url, an URL from which I will extract a substring off it, and my_id that will save that mentioned substring. my_url is a text_url field type, and my_id will be a custom field type, in this case, test_text_field. Note that the order of these fields is important, as my_id value depends on my_url value, and values will be saved in order from top to bottom as defined in our field definition file.

    So, when the post is saved, my_id will be sanitized. We don’t need its previous value, so we disregard it completely, and instead we place our own value, which will be extracted from the already saved my_url using a classic get_post_meta. Then, as our value is already “sanitized”, it will be saved.

    And that’s it. Thank you @tw2113 for your help and guidance, I hope this might help someone else who has this same issue.

    Thread Starter noquierouser

    (@noquierouser)

    I’m doing this code to find out why meta isn’t being saved in the post, and it’s quite strange. I’m trying this code, and it seems to save the meta key and value where I want it, but when I check the post afterwards, the data is gone. It’s not even saved in the DB.

    
    add_action("cmb2_override_my_url_meta_save", 'habsup_test_save', 10, 3);
    function habsup_test_save($args, $field_args, $field) {
      error_log(print_r(get_post_meta(get_the_ID())));
    
      update_post_meta(
        get_the_ID(),
        'test_id',
        str_replace('https://my.url/id/', '', $field_args['value'])
      );
    
      error_log(print_r(get_post_meta(get_the_ID())));
    }
    

    I’m thinking maybe this isn’t the right way to address the issue. Maybe using a custom field and hookins into the sanitize action might do the job?

    Thread Starter noquierouser

    (@noquierouser)

    I’m trying what you advised, but the value doesn’t seem to be updated. If I var_dump the update_post_meta, it returns the meta ID, as indicated by the documentation, but it doesn’t update the meta in the post. Here’s my code.

    
    add_action("cmb2_override_my_url_meta_save", 'habsup_test_save', 10, 3);
    function habsup_test_save($args, $field_args, $field) {
      update_post_meta(
        $field_args['id'],
        'test_id',
        str_replace('https://my.url/id/', '', $field_args['value'])
      );
    }
    
    Thread Starter noquierouser

    (@noquierouser)

    Thanks for the response, but I’m still a bit puzzled about how to save the second field value.

    Right now I’m doing this:

    
    add_action("cmb2_override_my_url_meta_save", 'custom_my_url_save', 10, 3);
    function habsup_test_save($args, $field_args, $field) {
      var_dump($field_args);
    }
    

    Which shows the values that are being saved for that field. But I can’t seem to do_action or apply_field for the field I want to save. How can I do?

    I don’t know if you figured it out or if you still need it, but here’s a fixed POT file so you can do the localization.

    https://www.mediafire.com/?i2nucn4e856z4pa

Viewing 5 replies - 1 through 5 (of 5 total)