Skip Navigation

[Résolu] Is it possible to set a default user/author when someone submits a content form?

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
How to set post author using CRED form dynamically

Solution:
You can use CRED hook cred_save_data hook to set the post author dynamically.

You can find proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/is-it-possible-to-set-a-default-user-author-when-someone-submits-a-content-form/#post-624191

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

This support ticket is created Il y a 6 années et 1 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 6 réponses, has 2 voix.

Last updated by Minesh Il y a 6 années et 1 mois.

Assisted by: Minesh.

Auteur
Publications
#622771

Tell us what you are trying to do? I built a front-end CRED form for submitting new custom post listing - camps - and would like to automatically assign the new listing/post to an existing user account - a house account.

Is there any documentation that you are following?
I've looked at the CRED documentation for front-end forms, and have done Google and forum searches, but haven't found what I've looking for.
Is there a similar example that we can see?
Not that I know of.
What is the link to your site? Here's a link to the form on the site: hidden link

#622822

Minesh
Supporter

Languages: Anglais (English )

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

Hello. Thank you for contacting the Toolset support.

You can use CRED hook cred_save_data hook to set the post author.

For example:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data){
// if a specific form
if ($form_data['id']==999) {
$my_post = array(
      'ID'           => $post_id,
      'post_author' => 7777
  );
   
// Update the post into the database
  wp_update_post( $my_post );
 
   }
}

Where:
- Replace 999 with your original CRED form Id
- Replace 777 with your desired post author ID

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

#623394

Minesh - this sounds perfect. But I've never worked with Hooks. Where do I place the code? I've read all your documentation and examples on using hooks and CRED hooks, but nowhere does it actually tell you where and how you place the code?

I tried using

 ... 

placed right before [cred_field field='form_submit' tag but it just displayed the code on the frontend.

Sorry - still learning.

#623397

Sorry - my last message didn't display correctly. I meant to indicate that I used the code tags as a container for the hook code you provided, but that didn't work.

#623597

Minesh
Supporter

Languages: Anglais (English )

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

You should add the code to your current theme's functions.php file.

#623782

Perfect!

And if I want to include multiple forms?

I'm assuming I can edit this line to include multiple form ID's?

if ($form_data['id']==999, ###) {
#624191

Minesh
Supporter

Languages: Anglais (English )

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

For multiple IDs you should use the following code:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data){
// if a specific form
if ($form_data['id']==999  OR $form_data['id']==888 ) {
$my_post = array(
      'ID'           => $post_id,
      'post_author' => 7777
  );
    
// Update the post into the database
  wp_update_post( $my_post );
  
   }
}

Where:
- Replace 999 and 888 with your original form IDs

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.