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 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.

This topic contains 6 réponses, has 2 voix.

Last updated by Minesh Il y a 3 années et 4 mois.

Assigned support staff: 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