Skip Navigation

[Resolved] assigning wordpress current user to a custom field using CRED

This support ticket is created 5 years, 3 months ago. 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.

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

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by martinE-4 5 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#1361475

Tell us what you are trying to do?
I'm creating a document manager (basically a CRED upload form and a VIEW to list the uploaded documents) to upload files and then list them with a view with different search criteria. I have a Cred form to add a new custom post of type 'document'. I want to assign the current user to a custom field so that I can search with a drop-down select on my view and display all 'document' posts with a given author (since I discovered searching of wordpress fields is not allowed by toolset on drop-down select).

So I created a types field called 'uploaded-by' to hold the current user who creates/authors the post. I thought that would be easiest to allow me to then search for all 'document' posts created/uploaded by a certain user.

Is there any documentation that you are following?
just learning about CRED hooks

Is there a similar example that we can see?
don't think so

What is the link to your site?
local

My action hook so far (not working):

add_action('cred_before_save_data', 'set_default_value_uploaded',10,1);
function set_default_value_uploaded($form_data)
{
    // if a specific form
     if ($form_data['id']== 18825)
     {
	 $current_user = wp_get_current_user();
	 $my_display_name = $current_user->display_name ;
// assign current user display name to types custom field 'uploaded-by'
		 $_POST['wpcf-uploaded-by'] = $my_display_name ;		
    }
}

Thanks so much.

#1361547

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Martin,

I'm assuming that the 'wpcf-uploaded-by' if a single line field.

In this case you will need to update the field like this in the hook.

add_action('cred_save_data', 'set_default_value_uploaded',10,2);
function set_default_value_uploaded($post_id,$form_data)
{
    // if a specific form
     if ($form_data['id']== 18825)
     {
     $current_user = wp_get_current_user();
     $my_display_name = $current_user->display_name ;
// assign current user display name to types custom field 'uploaded-by'
   
update_post_meta($post_id,'wpcf-uploaded-by',$my_display_name);
    }
}


Please try it like this and let me know if it helps.

Thanks,
Shane

#1361549

My issue is resolved now. Thank you!