Skip Navigation

[Resolved] Setting author based on post form submission

This support ticket is created 4 years, 1 month 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 4 replies, has 2 voices.

Last updated by matthewL-7 4 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1897077

Hi

I am currently using this code snippet I found on Toolset forums:

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

However I would like to only do this if the user is a guest, is this possible?

So if they are logged in, it assigns the post to them but if they are a guest then it assigns them to post_author ID 51.

As I need to set something otherwise it doesn't set anything and then when you go to edit it it just picks the first user in the list as the author when you go to save.

#1897277

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

As I understand, basically, what you want is, if user is loggedin, the author will be automatically set but if user submit the form as guest user, you want to assign specific author to that post. If this is correct, the code you shared will require little bit adjustment so that the code should be triggered only when user submit the form as guest user.

Can you please try to use the following code:
- You should try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_action('cred_save_data', 'func_set_custom_author_for_guest_user',10,2);
function func_set_custom_author_for_guest_user($post_id, $form_data){

    if ($form_data['id']==99999 and !is_user_logged_in() ){
            $my_post = array(
                        'ID' => $post_id,
                        'post_author' => 51
                        );
            // Update the post into the database
            wp_update_post( $my_post );
   
    }
}

Where:
- you should adjust the 99999 with your original form ID.

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

#1899345

Hey Minesh

Great this is working! I tested initially with the wrong form.

Is there a way to get this to target 2 form IDs at the same time?

I realised I could copy and paste this snippet and have it twice but I would imagine its better to just target the 2 IDs in the same snippet.

#1900789

Minesh
Supporter

Languages: English (English )

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

Yes, you can target the two form IDs.

For example:

add_action('cred_save_data', 'func_set_custom_author_for_guest_user',10,2);
function func_set_custom_author_for_guest_user($post_id, $form_data){
 
$forms = array( 99999, 88888, 77777 ); // replace with your original form IDs

  if ( in_array( $form_data['id'], $forms )  and !is_user_logged_in() ) {

    
            $my_post = array(
                        'ID' => $post_id,
                        'post_author' => 51
                        );
            // Update the post into the database
            wp_update_post( $my_post );
    
    }
}

Where:
- Pleaser replace 99999, 88888, 77777 with your original form IDs.

#1901283

Great thank you! 🙂