Skip Navigation

[Resolved] CRED Forms for Child Content on original parent post

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 11 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Tagged: 

This topic contains 21 replies, has 9 voices.

Last updated by Boris 11 years, 2 months ago.

Assisted by: Nikos.

Author
Posts
#36631

Ben

I've been able to follow the CRED Forms for Child Content ( https://toolset.com/documentation/user-guides/cred-forms-for-child-content ) guide to create a child post with the parent pre-selected.

It works just fine, but I'm not crazy about the user having to click a link on the parent post and go to another page with the cred form.

Ideally, I would like to have the cred form right on the parent page so the user can just enter information and the child post is created with the parent link established. (I know you can give the user an option to select the parent post, but I do not want my users to have that option. I need the parent association automatically created).

Using the book review site example, on the post for Book A, I would like to have the CRED form to add a review for Book A on that same page instead of just a link to "Review Book A". When the form is completed, the child post is created that automatically has the Book A post selected as the parent.

Is there anyway I can make this happen all on the same page?

#36640

Hello Ben, yes this can be done right now, with a little bit of hook magic.

Just create the child form and enter both forms (parent and child ) on same page

Then hook cred submit complete on child form (or even better cred_save_data) and set the parent to the current page id.

If you have both forms parent and child on same page only ONE form can be submitted at a time.

If what you mean is having the child form on parent page then similar scheme will work.

#36647

Ben

Nikos,

I'm not sure I follow.

So you're saying to put both the [cred-child-link-form ] (which originally linked to the new content form on a different page) and the new content form [cred-form] on the same page?

Two questions/concerns:

1. How would I set the destination of the [cred-child-link-form] to the current post? There will be a lot of different parent posts, so the destination page ID will be different every time.

2. You say that only one form gets submitted at a time. So a user would have to click on the link created by the [cred-child-link-form ] , which would reload the current page, and then the user could enter data into the child cred form and submit it? That still involves an extra link click that I want to avoid.

On the parent post, I want to have a cred form displaying just a "Review" input box and a submit button. I need the current page to be automatically included as the parent post without having to click a link (to another page or reloading the current page). Is this possible?

#36650

Hi Ben,
no need to use the [cred-child-link-form ] shortcode
just use directly the [cred-form form="Child Form"] shortcode (or through the cred popup)

This will render the child form directly in the page of the parent (if this is where you want it)

The only problem is that the parent will not be selected. So to solve this
you will use the cred hooks (check the CRED API) to set the parent of the child on Form submission,
to be the current parent, or whatever you like..

#36663

Ben

Nikos,

Ok, that is exactly what i want to do.

I'm just not sure how to use the CRED API. I read through the user guide, but still am not sure what to do. I'd really appreciate any help you can provide.

#36900

Hello Ben,
assuming the child form is inserted in the parent page, and the child form has an ID of 123
you can do this:

add_action(‘cred_save_data_123’, ’set_parent_to_child’,10,2);
function set_parent_to_child($post_id, $form_data)
{
global $post;

if (isset($post))
{
// add parent
// assuming current post is the parent
$parent_id=$post->ID;
$post_data=array(
'ID' => $post_id,
'post_parent' => $parent_id
);
// update post
wp_insert_post($post_data);
}

}

#36933

Ben

Nikos,

Thanks for the response, but I'm still having trouble.

The child form has an ID of 114, so I changed (‘cred_save_data_123’ to (‘cred_save_data_114’ and then added everything to my functions.php.

The form creates a child post, but the parent post is not assigned.

#36951

Hello Ben,

is the current post (that the child form is inserted into) the actual parent?

If so this should work, else in the code you have to give the parent_id explicitly
i am not sure how you have setup things

#36958

Ben

Yes, the current post is the parent custom post type "Books". On each book post, I have the Cred form to make a child post type "Reviews", which I want to be the child of the current post.

Any other trouble shooting suggestions?

#36961

Another solution would be to have the parent_id hardcoded into a shortcode
and use this shortcode as a value for the parent field in child form

eg:
add_shortcode('my_parent','my_parent');
function my_parent($atts, $content)
{
global $post;

return $post->ID;
}

Then in child cred form
try sth like this:

[cred-field field="_wpcf_belongs_myparent_id" value="[my_parent]"]
where myparent is the post type name of the parent (eg book)
maybe you can also set this to be hidden in the form, so eg wrap it in a div with display:none

#36964

Ben

Nikos,

That worked! Thanks for all the assistance.

Ben

#37028

Hi Ben,
the original solution should still work,
i just used the post_parent field instead of the types parent field, so if yuo use the types parent field as shown below,
first solution should work even better

add_action(‘cred_save_data_123’, ’set_parent_to_child’,10,2);
function set_parent_to_child($post_id, $form_data)
{
global $post;

if (isset($post))
{
// add parent
// assuming current post is the parent
$parent_id=$post->ID;
// update post, (types) parent field
update_post_meta($post_id, "_wpcf_belongs_myparent_id", $parent_id);
}

}

#122251

Its quite old and closed as well but for future users, my few cents.
You can also achieve same without coding.
There is also an existing short-code in views you can use it in child form.
Do not create a child content form and insert your child form directly into parent post template - like {cred_form form="Child form name"}
and in your child form use this

<div class="hide-parent">
      [cred_field field="_wpcf_belongs_parent_id" value="[wpv-post-id]" order="date" ordering="desc" no_parent_text="No Parent"]
</div>

and in css add display:none attribute.

Now when post is displayed it will have child form with pre-selected parent as current post. and also parent field be hidden.

#237141

This works perfect Mr.Salman. Thank you very much

#246247

This no longer work for cred 1.3.2
The parent field no longer pre-selected by assigned [wpv-post-id]

 [cred_field field="_wpcf_belongs_parent_id" value="[wpv-post-id]" order="date" ordering="desc" no_parent_text="No Parent"]

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.