Hi there,
I am struggling a little and wonder if anyone would be kind enough to point me in the right direction.
Here's the scenario:
1. I have a website that has Schools and Teachers. I have setup a relationship where one school can have many teachers.
2. I have a registration process:
2a. A teacher registers with a User Form - username / password
2b. I've got a function that when they hit register, they are automatically logged in
2c. The next Step is to present a form where they can search pre existing schools. If they find their school, they hit submit and they are assigned as a child to that school
2d. If they don't find their school, the next step presents them with a form to Create a new school. When they've done that, they should be assigned as a "teacher" of that school (but then further teachers can also be assigned to that school)
I'm struggling with step 2c onwards. Please can you help?
Many thanks
Hi,
Thank you for contacting us and I'd be happy to assist.
2c. After a user has registered using a registration form and logged in, the next step would be to create a "Teachers" post where the currently logged-in user is the post author.
Every user who registers on the website as a teacher will need to have exactly one "Teachers" post as a post author. That is because a relationship can only exist between two post types and not between user and post type.
The first form that a user will see after logging in will be a form to add a new "Teachers" post. In this form, the user will also have the "Schools" field to select for the teacher post being created.
2d. Add a new "Teachers" post form, will show only the existing "Schools" in its field. You can add another form to add a new "Schools" post, on the same page and add a note for users that if you don't see your schools in the existing schools' list, please use the other form to add your school first.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Hey there my friend.
Great response thanks! Yes, now I understand you've helped clarify perfectly - the moment you mentioned that Teachers post 🙂
Reading some other Docs on here, it seems to be best to do minimal user fields and then set them up as an author with additional fields. Then hook them in.
Thanks so much. May I leave this ticket open for a few hours just in case I need further assistance?
Thanks for the update and glad that my message helped.
You're welcome to mark this ticket as resolved, and start a new one for each new question or concern.
Hi there.
Just a quick question. I have a function that auto logs the user in and a seperate one to create a Teachers Post (See below).
Firstly, should I somehow combine these for code brevity or are they ok / better as two seperate functions.
Secondly, how do I add both First name and Last name fields as the post title? ( 'post_title' => $_POST['some_custom_input'],)
LOGIN USER AUTOMATICALLY WHEN THEY REGISTER
add_action( 'cred_save_data', 'tssupp_cred_autologin', 10, 2 );
function tssupp_cred_autologin( $post_id, $form_data ){
if ( 13246 == $form_data['id'] ) { // Edit as required
if ( !empty( $_POST['user_login'] ) && !empty( $_POST['user_pass'] ) ) {
// get the user credentials from the $_POST object
$user = array(
'user_login' => $_POST['user_login'],
'user_password' => $_POST['user_pass'],
'remember' => true
);
$login = wp_signon( $user, false );
if ( is_wp_error($login) ) {
error_log( $login->get_error_message() );
}
}
}
}
CREATE NEW POST ON THE SAME FORM WHEN SUBMITTED
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
if ($form_data['id']==13246)
{
$my_post = array(
'post_title' => $_POST['some_custom_input'],
'post_status' => 'publish',
'post_author' => $post_id,
'post_type' => 'teacher'
);
// Insert the post into the database
wp_insert_post( $my_post );
}
}
One further thing. In the second code, should I use
'author' => $current_user->ID,
Or
'post_author'=> $current_user->ID,
Instead of
'post_author' => $post_id,
Another issue is I can't get the form to show no matter what I try - even looked in Access and enabled for the user role the new user gets added to:
[cred-relationship-form-container]
<div class="form-group">
<label for="%%FORM_ID%%_parent">[cred_i18n name='parent-label']Schools[/cred_i18n]</label>
[cred-relationship-role role='parent']
</div>
<div class="form-group">
<label for="%%FORM_ID%%_child">[cred_i18n name='child-label']Teachers[/cred_i18n]</label>
[cred-relationship-role role='child' author='$current']
</div>
[cred-form-feedback field='feedback' name='feedback']
[cred-form-submit field='submit' name='submit']
[cred-form-cancel field='cancel' name='cancel']
[/cred-relationship-form-container]
Hi there,
Ok I must have been adding the wrong form. I can now get it to work for a Teacher becoming a child of a school. However it looks a bit clunky in the fact that they've just registered and have to search for themselves on the second Select menu,
Is there anyway that this can be set to the current logged in user automatically?
i.e. this field:
<div class="form-group">
<label for="%%FORM_ID%%_child">[cred_i18n name='child-label']Teachers[/cred_i18n]</label>
[cred-relationship-role role='child' author='$current']
</div>
And just one more thing (apologies - I sound like Columbo )
If a Teacher can't find their school and I add a form for them to create a new School, how do I make sure they are also set as the Child of that school as well as Parent - is that another relationship form and handled automatically?
Many thanks for all the help I'm almost there I think!
Hi there,
Also I can't find anyway to show fields from School on a teachers post. I want to show fields displaying info about the school they are a child of. Nothings displaying and I've checked and verified the Access plugin...
Thanks for writing back and I'll answer all your questions, point-by-point.
1. It is a matter of personal preference, whether you'd like to keep the two code snippets or combine them as one.
If you decide to combine the two functions, you can move the code to insert the new post, in the first function, within the condition that checks for a specific form.
( screenshot: hidden link )
2. If your user form includes the fields for the first name and the last name, you can use their text for the newly created post's title, like this:
'post_title' => $_POST['first_name'].' '.$_POST['last_name'],
3. As it is user form, the "$post_id" will hold the ID of the newly created user, and using it for the post's author value should work fine:
'post_author' => $post_id,
As for the remaining questions, they need further details so I'm going to follow up on them using separate tickets.