Skip Navigation

[Closed] Using a different forms plugin to submit custom fields?

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by Timothy 1 year, 3 months ago.

Assisted by: Nigel.

Author
Posts
#2640003

Rather than using the Toolset forms plugin, can I use a different forms plugin (formidable forms) and create a new post (to a custom post type) and map the fields into custom fields created with Types?

Thanks,

Tim

#2640049

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

This should be okay for simple fields, by which I mean fields whose values are essentially just stored as a string, which is most fields (things like email and url fields as well as simple select fields).

There are a few gotchas.

One is that Types custom fields are saved in wp_postmeta with a 'wpcf-' prefix, so if you have a custom field with a slug of "priority" then that is actually saved with a post meta key of "wpcf-priority". So when adding fields to a different form you would need to include the wpcf- prefix.

Two would be date fields, which are converted to a timestamp before saving to the database. Your form builder might handle date fields differently, in which case you would likely need to add some code to handle the form submission to convert the date to a timestamp before saving.

#2640117

Thanks. I don't need date fields so that would not be an issue. But I'm still uncertain how I implement this. Do I just need to make sure the name/slug of the field in formidable forms is the same as the name I use in toolset (and adding the wpcf-)? There must be more to it than that right? How does the form know to create a new post?

Also, would this work for a checkboxes field?

#2640167

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I'm not particularly familiar with Formidable Forms, I assume you can create posts with them, and include fields that will generate custom fields for the submitted post, but you'd have to refer to their documentation for clarification about that.

Assuming it can, then—yes—it should just be a matter of telling it that the field key should be something like 'wpcf-priority' for a field registered with Types with a slug of 'priority'.

The checkboxes field is a no-no. It's the most complex kind of field available in Types.

If your Formidable Form had a checkboxes field (or similar field type that allowed you to select multiple options) then one option would be to add some code to process the form submission (again, you'd need to consult the Formidable documentation about that), and you could try to update the options of a Types checkboxes field using an unofficial API function I'll share below. Some programming is unavoidable, though, if you want this.

/**
 * Unofficial API function to check/uncheck checkboxes options
 * 
 * @param int $post_id
 * @param string $field // slug of checkboxes field
 * @param string $option // title of checkbox option to manipulate
 * @param string $action : 'check' | 'uncheck' | 'value' (default, returns current value)
 * 
 * Important: assumes recommended checkboxes setting of save nothing to database when unchecked
 */

 function ts_checkboxes( $post_id, $field, $option, $action = 'value' ){

    if ( isset($post_id) && isset($field) && isset($option) ){

		$field_settings = types_get_field( $field );
		
        $field_options = $field_settings['data']['options'];

		// Locate the option key
        $key = array_search( $option, array_column( $field_options, 'title' ) );
        $keys = array_keys( $field_options );
		$option_key = $keys[$key];

		// Get the current post meta value
		$meta = get_post_meta( $post_id, 'wpcf-'.$field, true );
		if ( !is_array($meta) ){
			$meta = [];
		}

		// If action = 'value' just return the value
		if ( $action == 'value' && isset( $meta[$option_key] ) ){
			return $meta[$option_key][0];
		} else {
			// Remove the existing key if it exists (i.e. uncheck)
			// because recommended setting is to save nothing when unchecked
			// if ( is_array($meta) ) {
				unset( $meta[$option_key] );
			// } else {
			// 	$meta = [];
			// }
			
			// If $checked == true then add back the key + value
			if ( $action == 'check' ){
				$meta[$option_key] = array($field_options[$option_key]['set_value']);
			}
			update_post_meta( $post_id, 'wpcf-'.$field, $meta );		
		}
    }
}

Examples of how you would use the above function:

// 1. Get the value of the option "Second checkbox" of a checkboxes field "Available options" from the current post:
global $post;
$value = ts_checkboxes( $post->ID, 'available-options', 'Second checkbox' );
 
// 2. set the same option as checked
global $post;
ts_checkboxes( $post->ID, 'available-options', 'Second checkbox', 'check' );
 
// 3. set the same option as unchecked
global $post;
ts_checkboxes( $post->ID, 'available-options', 'Second checkbox', 'uncheck' );
#2640551

Thanks for this. With the checkbox field being so complicated I may just stick with using Toolset forms.

Tim

The topic ‘[Closed] Using a different forms plugin to submit custom fields?’ is closed to new replies.