Skip Navigation

[Closed] Modify input value before it is submitted

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 9 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 6 replies, has 4 voices.

Last updated by Waqas 9 years, 6 months ago.

Assisted by: Waqas.

Author
Posts
#251562

I'm trying to hook onto the Types API to change a field value from human-readable time (4:32) to seconds (272) but cannot find a hook to seamlessly change the output on the admin interface and then change it back in the backend (without having to query the db to get the value, change it then save it back anyway).

I have two option:
1. Hook into the Types API and change the value seamlessly (preferred);
2. Create a new Custom Field Type.

How do I do either of them? My last option would be to use another field to store the numbers and convert the human-readable time to seconds with JavaScript, but I really don't want to go down this path.

#251579

Dear Timothy,
Yes I also prefer using the Types API method. However, its unclear to me the specific process flow of this data processing aside that you have mention about having it in the admin interface and then changing it back on the backend. It's quite broad. Can you please provide a screenshot of the following?

1.) Screenshot where you would like the human-readable time (4:32) to be shown.
2.) Screenshot where you would like the converted seconds (272) to be shown.

If you can provide more screenshots of your process flow, that would be great. This help us understand your expectations better so we can check this on our end and replicate to see what's possible.

If you have some existing PHP codes that you are trying to test to fix this but still not working, feel free to share. Thanks a lot! 🙂

Cheers,
Emerson

#251581

Show human-readable form in the admin panel when editing; store in database as numeric. Can't get more specific than that.

#251594

Dear Timothy,

You must use two filters:

wpcf_fields_slug_(slug)_value_get - to modify value before showing it in form
wpcf_fields_slug_(slug)_value_save - to modify value before saving in database

Example for field with slug "time-in-second":

/**
 * example for field with slug time-in-second
 */


add_filter( 'wpcf_fields_slug_time-in-second_value_get', 'my_time_in_second_get', 10, 2 );

function my_time_in_second_get($meta, $field)
{
    /**
     * return if $meta is empty
     */
    if (empty($meta)) {
        return $meta;
    }
    $meta = intval($meta);
    $min = intval($meta/60);
    $sec = $meta - $min*60;
    return sprintf('%d:%02d', $min, $sec);
}

add_filter( 'wpcf_fields_slug_time-in-second_value_save', 'my_time_in_second_save', 10, 3 );
function my_time_in_second_save($meta, $config, $field)
{
    /**
     * return if $meta is empty
     */
    if (empty($meta)) {
        return $meta;
    }
    /**
     * check format
     */
    if ( preg_match('/^(\d+):(\d+)$/', $meta, $matches ) ) {
        return $matches[1] * 60 + $matches[2];
    }
    return $meta;
}

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have

Marcin

#251615

That did the trick. 🙂

Just for future Googler's sake, what is $field?

#253468

This just popped up: I don't know why, but it seems that the code around apply_filters('wpcf_fields_slug_(slug)_value_get'); is not called for me. I tried to die(); right before the apply_filter but WordPress would not die. The metaboxes still render, but the filters are not applied.

#259628

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

This is Waqas here, I will be handling this thread now.

Based on the solution provided by Marcin (above), I have just checked it on my local host. I have found it working perfectly fine.

However, can I ask you for a temporary access to your website? So I can look into more details. I have set your next reply as private, please input all private information in that area. Also please remember to take backup of your word press (website + data) before sending the access.

Please let me know if I can help you with anything related.

The topic ‘[Closed] Modify input value before it is submitted’ is closed to new replies.