Skip Navigation

[Resolved] Set post author dynamically as current loggedin user when you submit the toolset form and generate post title

This thread is resolved. Here is a description of the problem and solution.

Problem:
Set post author dynamically as current loggedin user when you submit the toolset form and generate post title

Solution:
You can use the Toolset Form's hook "cred_save_data" to set the post author and generate the post title dynamically after you submit the form.

You can find proposed solution, in this case with the following reply:
https://toolset.com/forums/topic/contractor-custom-type-and-ban-change-of-author/page/2/#post-2228759

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 3 years 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
- 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)

This topic contains 17 replies, has 2 voices.

Last updated by alexG-11 3 years ago.

Assisted by: Minesh.

Author
Posts
#2227089

Tell us what you are trying to do?

We are trying to create a directory of a type of role.

We are following this Toolset docs:
https://toolset.com/documentation/legacy-features/views-plugin/how-to-create-custom-searches-and-relationships-for-users/
https://toolset.com/documentation/customizing-sites-using-php/creating-custom-user-profiles/

The problem is the "editors" and the "admin" has to be hable to edit the Contractor Post Type. But in this moment when they do this, the post type change of author, and broke the link between the original author and the contractor post type. And that's not what we need.

Any possibility of mantain the original author link? For display some info of it in the single and the views?

#2227505

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please explain when you say:

But in this moment when they do this, the post type change of author, and broke the link between the original author and the contractor post type. And that's not what we need.

Do you mean that when you change the post author for any specific single Contractor post - what issue you are having. Can you please explain with bit more details or a example so I can guide you in the right direction.

#2227609

If and admin or a editor edit the contractor this custom type loss the original author and the admin or the editor is the author in this moment. We don't want this we need the original author remains as the only author. If not the link is between the original author and his/her contractor IS broken.

#2227621

We need this for the "edit form view", the contractors loop and the email of edit response.

#2227657

Minesh
Supporter

Languages: English (English )

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

Can you please share problem URL where I can see the issue and steps to see the issue.

*** 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 have set the next reply to private which means only you and I have access to it.

#2227703

You have the temp access to the ftp and the wordpress.

Thanks for your help

#2227709

Minesh
Supporter

Languages: English (English )

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

Thanks for sharing the admin access details.

Can you please share problem URL and steps that I should follow that could lead me to see the issue.

#2227765
2021-11-23 12_09_38-Window.png
2021-11-23 12_07_31-Window.png

Hi again

1 . The user acces to this form in "registre" (Image 2021-11-23 12_07_31)
2. When the form is filled the user acces acces to this form with an auto login. (Image 2021-11-23 12_09_38)
3. Then we need to aprove and probably modify the content by an editor or and admin in the backend. When this happen the autor doesn't have to change, in this moment or in the futures if we need to edit the reedited content by the real user.

We need this for give the user the possibilty of edit his profile and maintain the comunication with his email.

#2227805

Minesh
Supporter

Languages: English (English )

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

As I understand now, when user interact with the following form shown with the following image:
=> hidden link

And when they do modifications to that form as a admin or editor user - you want that the author of the that profile should remain as the loggedin user who created the profile first - is that correct?

#2227887

Yes that is! We want the loggedin user remains as the creator of the profile.

#2227891

Minesh
Supporter

Languages: English (English )

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

Thanks for the confirmation.

I tried to login to admin using the admin access details you shared but unfortunately its not working at this end. Can you please send me working admin access details.

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

#2228705

Minesh
Supporter

Languages: English (English )

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

So I register my self using the following form: hidden link

Once I register it redirect me to the following page: hidden link

Now, I should fill the profile and save it and at that time the author of the post is the current loggedin user that you want - right?

And then what is the flow to edit the same profile using admin or editor?

#2228733

Before that the text is pendding and not published since a editors or an admins moderate the text and images (in the wordpress admin panel / backend)and publish if all is ok. Sending in this moment a email to the contractor author with a comunication.

The problem is in this moment the contractor author change to the moderator. And that not what we need.

Before that the author has to be able to reedit his contractor type and the cicle restars again with the moderator, the comunication, etc.

If the author change we can't create a update form for the author in the frontend and the email link is broken.

#2228759

Minesh
Supporter

Languages: English (English )

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

Ok - as I understand, the flow is:
- user registers and add his profile using the following form: hidden link
- Once profile is submitted - admin or editor user will review it in the backend and the publish it
- so, when admin or editor user publish the profile after reviewing it from backend, when they publish it it changes the post author to admin or editor user and thats the issue - thanks for all clarifications.

So, when I checked your theme's functions.php file I see you already added the "cred_save_data" hook. I've adjusted the hook code as given under and added the post_author argument:

add_action('cred_save_data', 'build_post_title', 10, 2);
function build_post_title($post_id, $form_data) {
  
    if ($form_data['id']==45) {
        $tipus = get_post_meta($post_id, 'wpcf-tipus', true);
        
        if($tipus == "artista"){
            $title = get_post_meta($post_id, 'wpcf-nom-autor', true).' '.get_post_meta($post_id, 'wpcf-cognom-1', true).' '.get_post_meta($post_id, 'wpcf-cognom-2', true);
        }else{
            $title = get_post_meta($post_id, 'wpcf-nom-empresa', true);
        }
    
        // generating slug 
        $slug = sanitize_title($title.$post_id);
    
        wp_update_post(array('ID'=>$post_id,'post_author' => get_current_user_id(),'post_title'=>$title,'post_name' => $slug));
    }
}

As you can see with the following link:

wp_update_post(array('ID'=>$post_id,'post_author' => get_current_user_id(),'post_title'=>$title,'post_name' => $slug));

Can you please confirm as we already set the post author and now when you edit the post from backend it works as expected.

#2228857

Thank you Minesh!
It works.