Skip Navigation

[Closed] Update related Author post on CRED Edit User form

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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Shane 1 year, 10 months ago.

Assisted by: Shane.

Author
Posts
#2383771
Screenshot 2022-06-13 at 12-36-51 Dashboard - 360 Skills For Life CIC.png
Screenshot 2022-06-13 at 12-37-01 Janice Allen - 360 Skills For Life CIC.png

Hi there, I would really appreciate some help - can you point me in the right direction?

On my website, the Admin creates a User. On saving that new user, I have a function that creates a new CPT post called Teacher with the same details as the newly created user - i.e. First Name, Last name, Email.

The proble I now face however, is that the Admin wants to be able to Edit the user through CRED and not only update the USER fields but also the CPT Post TEACHER with the new details (i.e. different name and email).

Please see attachments (screenshots) and code for context:

This code Creates the New Teacher post intially. Is there something I can copy from here for the Save data on the CREDE Edit form. A user will only have ONE Teacher post assigned to them:


// Add relationship between author and child (School Lead and new teacher).
add_action('cred_save_data', 'my_save_data_action_teacher',10,2);
function my_save_data_action_teacher($post_id, $form_data)
{
	  $form_id = array( 15530 );
	 if ( in_array( $form_data['id'], $form_id ) ) {
    {
		$current_user_id = get_current_user_id();
	$first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
	$user_email = $_POST['user_email'];	
	$the_role = get_user_meta( $post_id, 'user_role', true );
	$new_title = $first_name . " " . $last_name;
    $new_title = sanitize_text_field( $new_title );
      $my_post = array(
        'post_title'    => $new_title,
        'post_status'   => 'publish',
        'post_author' => $post_id,
        'post_type' => 'teachers'
      );
          
      // Insert the post into the database
      // Insert the post into the database and save new Client post ID
$new_post_id = wp_insert_post( $my_post );
 
// Update any custom fields on Client Post
update_post_meta( $new_post_id, 'wpcf-first-name', $first_name );
update_post_meta( $new_post_id, 'wpcf-last-name', $last_name );
update_post_meta( $new_post_id, 'wpcf-teacher-email', $user_email );
update_post_meta( $new_post_id, 'wpcf-user-id', $post_id );
update_post_meta( $new_post_id, 'wpcf-teacher-role', $the_role);
	}
	 }
}

Many thanks for any help

#2383939

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Paul,

Thank you for getting in touch.

I believe the same setup will work for the edit form, however instead of using the wp_insert_post() function to create the new teacher post. You will need to use the wp_update_post instead in order to update the existing post.
https://developer.wordpress.org/reference/functions/wp_update_post/

Please let me know if this helps.
Thanks,
Shane

#2384025

Hey Shane,

Thanks so much for your help. So on the CRED function for Save data on my User Edit form, would I need to get the post type I'm trying to update - how would I make sure it only updates the one related to the specific User being edited in the frontend form?

I think I can put together the rest just need a little help with that first bit

Paul

#2384083

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2022-06-13 at 2.22.58 PM.png

Hi Paul,

It might be a little tricky to achieve, however here is the general documentation on the subject.
https://toolset.com/course-lesson/front-end-forms-for-editing-content/?utm_source=plugin&utm_medium=gui&utm_campaign=forms

The concept is essentially the same, the only difference is that it will be a user form instead of a post form.

Now to get it to edit the actual users is that you will need to use the Classic views setup to list out all the users with the specific role.

Then add the edit user link to that view beside the user's name after creating the view. See Screenshot

Please let me know if this helps.
Thanks,
Shane

The topic ‘[Closed] Update related Author post on CRED Edit User form’ is closed to new replies.