userswp icon indicating copy to clipboard operation
userswp copied to clipboard

Custom fields won't save to the account form.

Open johnkawakami opened this issue 1 year ago • 0 comments

When I make a custom field, it won't save to the form.

It can be fixed by going into the database, into the uwp_form_fields and setting the 'form_type' field to 'account'.

It appears that the problem is that the form_type value is being lost somewhere. The process of creating the form field requires the form_type=account to be passed around, from the server to the client, then back to the server.

I'm not sure if this is the problem line. I'm still working this out, tracing the process.

https://github.com/AyeCode/userswp/blob/68beb46e3b57ff0842af29d7ce26dfacf251e98f/admin/settings/class-formbuilder.php#L1128


I wrote some code to make a Custom Field. Here's the code. It's just a copy of the Twitter custom field, but altered slightly to create a BlueSky field.

add_action( 'init', function() {
	add_filter( 'uwp_form_fields_custom', 'custom_form_fields_custom', 10, 1 );
});

function custom_form_fields_custom( $custom_fields ) {
$custom_fields['bluesky'] = array(
	'field_type' => 'url',
	'class'      => 'uwp-twitter',
	'field_icon' => 'fab fa-twitter-square',
	'site_title' => __( 'BlueSky', 'userswp' ),
	'help_text'  => __( 'Let users enter their bluesky url.', 'userswp' ),
	'defaults'   => array(
		'admin_title'   => 'BlueSky',
		'site_title'    => 'BlueSky',
		'form_label'    => __( 'BlueSky url', 'userswp' ),
		'htmlvar_name'  => 'bluesky',
		'is_active'     => 1,
		'default_value' => '',
		'is_required'   => 0,
		'required_msg'  => '',
		'field_icon'    => 'fab fa-twitter',
		'css_class'     => 'btn-twitter',
	)
);
}

That works correctly to display a new custom field in the "custom field" section of the form builder at wp-admin/admin.php?page=uwp_form_builder.

The value of form_type from the server side appears to be exposed via some HTML that's generated, and is wrapped in a code block.

On the client side, it extracts this value, and uses it to produce a GET request to the admin-ajax.php service like this:

https://example.com/wp-admin/admin-ajax.php?action=uwp_ajax_action&create_field=true&htmlvar_name=friendster&field_type=url&field_type_key=friendster&field_id=new281&field_ins_upd=new&manage_field_type=custom_fields&custom_type=custom&form_id=1

The selector for the element is #form-type, and there are 6 instances on the page. 5 of them have a value of "account", and one, BlueSky custom field, has a value of "".

This line jumped out at me because it's not passing $form_type to the method.

https://github.com/AyeCode/userswp/blob/68beb46e3b57ff0842af29d7ce26dfacf251e98f/admin/settings/class-formbuilder.php#L1128

Further down, this code generates the HTML for the custom field.

https://github.com/AyeCode/userswp/blob/68beb46e3b57ff0842af29d7ce26dfacf251e98f/admin/settings/class-formbuilder.php#L1166

It should set the form-type to 'account', but, because it's not passed, it's set to the default value of null.

I'm going to keep working on this.

johnkawakami avatar Nov 14 '25 02:11 johnkawakami