Skip Navigation

[Resolved] Create a post on user registration and set the user as the author

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 7 replies, has 2 voices.

Last updated by Valeriia 3 years, 9 months ago.

Assigned support staff: Minesh.

Author
Posts
#470786

Hello,

I need to create a custom post type on user registration. How do I set this user to be the author of the post?

Thanks.

#470971

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - as I understand you want to set the logged in user as the author of the post created using CRED form. If this is correct then, to set the post author on CRED form to submit action you can use CRED hook: cred_save_data

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']==12)
  
{
$my_post = array(
      'ID'           => $post_id,
      'post_author' => 1111
  );
 
}
}

Where:
- Replace 9999 with your CRED form ID.
- Replace 1111 with post author ID.

More info:
=> https://toolset.com/documentation/user-guides/cred-api/#csd

#470975

Hi Minesh,

Thanks for the reply.

Not exactly. I missed a part of the explanation, my bad.

The situation is following:

I'm creating a sort of pen pal service where users can register and save an introduction message for potentials pen pals. These messages along with some information about the author are displayed on front end, so other users can filter (by custom user fields I added) and choose who do they want to write.

It would be enough to use user profiles to display these messages on the front end, but your Views plugin doesn't provide filtering options for the users. So I decided I will create a post for each user and display them.

So my question is: how to create a post on user registration and make that user its author?

My guess was to use cred_submit_complete action, but this is as far as I went.

Could you advise?

Thanks.

#470982

Minesh
Supporter

Languages: English (English )

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

Yes both cred_save_data and cred_submit_complete hooks should work. You need to add your custom code within this hooks and need to create a new post and assign your registered user as your new post author.

#470988

I managed, thanks for your time. 🙂

#470997

Quick question:

Now I'm editing a user account and want to update post data on sumbit (for the post where this user set as an author).

I have the following code. I can update "status", but "language" does not change.
Language is set as checkboxed (multiple choice), status as a single checkbox.

add_action('cred_submit_complete', 'my_save_data_action2',10,2);
function my_save_data_action2 ($post_id, $form_data) {
    if ($form_data['id']==428) { 
   		$related_post = get_user_meta( $post_id, 'wpcf-related-post', true );
		update_post_meta( $related_post, 'wpcf-post-language', $_POST['wpcf-language'] );
                update_post_meta( $related_post, 'wpcf-post-status', $_POST['wpcf-status'] );
    }
}

Should I use a different technique with multiple checkboxes?

Thanks for the advice.

#471013

Minesh
Supporter

Languages: English (English )

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

Well - checkboxes fields are special fields stored as serialized arrays.

You could try to follow the following way:
=> https://toolset.com/forums/topic/update-checkboxes-checked-state-via-php/#post-235982

#471167

Figured, thanks for your time.