Skip Navigation

[Gelöst] prepolulate parent field in cred form

This support ticket is created vor 7 Jahren. There's a good chance that you are reading advice that it now obsolete.

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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Dieses Thema enthält 7 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von Matthias Reichl vor 7 Jahren.

Assistiert von: Minesh.

Author
Artikel
#589604

I am trying to:
Use CRED Foms to create event participations of logged in users via a link from the event
I followed your recipy under https://toolset.com/documentation/user-guides/cred-forms-for-child-content/
You are using the CPT: book, reader, review (child of the other two)
I use the CPT: event, person, participation (child of the other two)
The IDs of "persons" are identical to the IDs of the users

I managed to pre-polulate the event with the cred child form link

However, I have no idea how to pre-populate the person parent field with the ID of the currently logged in user

Obviously you did this (preselecting the reader) on your example above, but did not document it.

my cred form code is:

[credform class='cred-form cred-keep-original']

	[cred_field field='form_messages' value='' class='alert alert-warning']

	<div class="form-group">
		<label>Anmeldung-Name</label>
		[cred_field field='post_title' post='anmeldung' value='xxx' urlparam='' class='form-control' output='bootstrap']
	</div>

	<div class="form-group">
		<label>Übergeordnet zu person</label>
		[cred_field field='_wpcf_belongs_person_id' value='[wpv-current-user info='ID']' class='form-control' output='bootstrap']
	</div>
	<div class="form-group">
		<label>Übergeordnet zu veranstaltung</label>
		[cred_field field='_wpcf_belongs_veranstaltung_id' value='' select_text='--- not set ---' class='form-control' output='bootstrap']
	</div>

	[cred_field field='form_submit' value='Einsenden' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']

[/credform]

Link to a page where the issue can be seen:
versteckter Link > click on register

I expected to see: also the person pre populated

Instead, I got: the person as select box

#589670

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand you just want to prepopulate the parent "_wpcf_belongs_person_id" with current user id and your aim is to not to display this field but save the field automatically having current user id - correct?

If this is true - you should add generic hidden field with same field name:

[cred_generic_field field='_wpcf_belongs_person_id' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"default":"[wpv-current-user info='id']"
}
[/cred_generic_field]

Could you please try to use above field instead of your standard field that holds parent "_wpcf_belongs_person_id" and try to resolve your issue.

More info:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

#589696

Hi Minesh!

Generally this works, however, I had a conceptional error in my approach.

I have a CPT "person".
Each "user" is associated to a "person" via a field with the slug "benutzer-id" in the CPT "person". This field holds the ID of the associated "user"
I have to use this approach because a "user" cannot be a parent of a CPT.

So the ID I would have to store under _wpcf_belongs_person_id is:
- the ID of the "person" associated to the currently logged in "user"
- and not the ID of the currently logged in "user"

I suppose I need a function to retrieve the ID of the "person" associated to the currently logged in "user" and put this value into the cred form.

Could you please help me with this function?

#589709

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Yes - that is correct, you need to write such shortcode or function to retrieve that associated user_id.

Could you please tell me the benutzer-id is stored as custom user field or where exactly we can find it. If you can share test example where I can see the user_id for person which you want to reference to cred field I can help you to write such shortcode or function.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#589854

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Thanks for sharing all information. But could you please stop editing as I try to edit CRED form but it says the admin is editing currently and having all rights so I'm not able to make any changes.

Could you please stop editing or logout for now, so I can start checking further.

#589877

OK, just logged out.

#589889

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

I've build a custom shortcode that return the post person ID based on the current logged in user which will compare the custom user field in person post = cureent user id.

I've added that code to your theme's functions.php file as that is the recommended way to add any custom code:

function func_fetch_person_id() {
	 $current_user = wp_get_current_user();
	
 
	$args= array(
				'post_type' => 'person',
				'numberposts' => -1,
				'meta_query' => array(array('key' => 'wpcf-benutzer-id', 'value' => $current_user->ID)));
	$person = get_posts($args);
    if(count($person) > 0){
     return $person[0]->ID;
    }
    		
}
add_shortcode( 'get_person_id', 'func_fetch_person_id');

Then I assigned the shortcode to your generic field and I can see that it has person post value:

[cred_generic_field field='_wpcf_belongs_person_id' type='hidden' class='' urlparam='']
	{
	"required":0,
	"validate_format":0,
	"persist":1,
	"default":"[get_person_id]"
	}
	[/cred_generic_field]

Could you please confirm it works for you as well.

#589897

Hi Minesh!

Thank you a lot - you are my hero!