Skip Navigation

[Resolved] Dynamic select input in a cred form

This thread is resolved. Here is a description of the problem and solution.

Problem:
I have a cred form, for the current user to create answers and this select input is used to choose the parent:

[cred_field field='_wpcf_belongs_sample_id' value='' class='form-control' output='bootstrap']

I need the following functionality:
Current user should not given the ability to create an answer for a sample, he/she has already created. This way, select inputs would become less and less, until zeroed.
So, for each item of the select input: Show parent post, if there is no child created by the current user.

Solution:
We need to add following code in theme’s functions.php file. When user submits CRED form, it will get sample post ID from our custom dropdown and save it as parent in the submitted post using cred_save_data hook.

add_action('cred_save_data', 'prefix_my_cred_cred_save_data_action', 10, 2);
function prefix_my_cred_cred_save_data_action( $post_id, $form_data ){
    if ($form_data['id']==39){ //do the following code only if the CRED form ID is 39, adjust this to your CRED form ID
        if (!empty($_POST['newparent'])){ //if parent is selected
            $parent_id = $_POST['newparent'];
            add_post_meta($post_id, '_wpcf_belongs_sample_id', $parent_id, true); //save the ID of the sample as parent of the current post
        }
    }
}

==> Replace ‘39’ with your CRED form id.
==> Please go through above code, you might also want to change something else in that (field name, etc).

1. You can add your shortcode directly in cred form.

<div class="form-group">
        <label>Give your answer</label>
    [custom_new_sample_select]
</div>

2. If you find any issue then please don’t do directly ‘echo’ in your custom shortcode. First save them in some variable and then use return statement as follows.
Following Code:

$content = '<select name="newparent" class="form-control" output="bootstrap">';
    foreach ($notanswered_sample_list as $post) :
        $content .='<option value=' . $post->ID . '>' . $post->post_name . '</option>';
    endforeach;
$content .='</select>';
wp_reset_postdata();
return $content;

Relevant Documentation:
https://codex.wordpress.org/Shortcode_API
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 7 years, 2 months 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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#567879

Hello,

I am building a membership site and I use two CPTs, samples and answers which are children of samples.
I have a cred form, for the current user to create answers and this select input is used to choose the parent:

[cred_field field='_wpcf_belongs_sample_id' value='' class='form-control' output='bootstrap']

Everything works fine, but I need the following functionality:
The select input should not list parents for which children are already made by the current user. In other words, current user should not given the ability to create an answer for a sample, he/she has already created. This way, select inputs would become less and less, until zeroed.

So, for each item of the select input: Show parent post, if there is no child created by the current user.

Any ideas on how to realize this?

Thank you in advance,
Kostas

#568007

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Kostas,

Thank you for contacting Toolset support. Using builtin Toolset options this might not be directly possible to do. So to achieve this you would need to make a custom shortcode to add a Custom parent dropdown in the CRED form and use our CRED API to save the data.

In the custom shortcode we need to define following functionalities:

1. We need to get current user id
https://developer.wordpress.org/reference/functions/get_current_user_id/
2. Then we need to get all child (answers) posts of above user
https://codex.wordpress.org/Class_Reference/WP_Query
3. And then get all parent ids from above results and store in array.
4. After that we need to get all parent (Sample) post except above store ids.
5. We will make custom select/option dropdown using above Sample posts.
6. Now we have just those Samples that are not created by current user.

For saving custom parent dropdown:
- When user submit cred form we need make parent by using “cred_save_data” filter hook.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Thank you

#568164

Hi Noman,

Thank you for responding.
Two questions please:

1. If I make a custom shortcode to add a Custom parent dropdown (as you described) every time the CRED form is loaded, then why I need to use the "cred_save_data" filter hook?

2. Where is the documentation on how to make a custom shortcode?

Cheers,
Kostas

#568654

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello,

1. cred_save_data is used to save the data coming from your custom shortcode or function, so that it is saved like other cred data. As the doc says:

“ This hook allows doing a custom action when post data is saved to database. “

2. Shortcodes needs to be created using standard WordPress coding standards:
https://codex.wordpress.org/Shortcode_API
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/
hidden link

Please note that above info is specially for someone having WP programming experience. And that we cannot consult on custom code as this is out of support policy (https://toolset.com/toolset-support-policy/). We only support builtin features of Toolset. For this, you can contact Toolset recommended service providers to further discuss the custom approach. We have some recommended list of service providers here if you would like to take a look: https://toolset.com/consultant/

Thanks

#569070

Hi Noman,

Thank you for the guidance!

I have built up the following custom shortcode in my functions.php, which prints a custom parent dropdown and is working fine.

/**** Create custom shortcode [custom_new_sample_select] ****/
function custom_new_sample_select_func(){
	$current_user = wp_get_current_user();
	$userid = $current_user->ID;

	$args1 = array(
    	'post_type' => 'answer',
    	'author' => $userid,
		'nopaging' => true
	);
	// Return an array of all answers of current user
	$user_answer_list = get_posts($args1);

	$answered_sample_list = array();   // create an array to hold user's answered samples IDs

	foreach ($user_answer_list as $post) : 
		$parentid = get_post_meta($post->ID, '_wpcf_belongs_sample_id', true);
		$answered_sample_list[] = $parentid;   // populate the array
	endforeach;

	wp_reset_postdata();

	$args2 = array(
    	'post_type' => 'sample',
		'exclude' => $answered_sample_list,
		'nopaging' => true
	);
	$notanswered_sample_list = get_posts($args2);

	echo '<select name="newparent" class="form-control" output="bootstrap">';

	foreach ($notanswered_sample_list as $post) :
		echo '<option value=' . $post->ID . '>' . $post->post_name . '</option>';
	endforeach;

	echo '</select>';

	wp_reset_postdata();
}
add_shortcode( 'custom_new_sample_select', 'custom_new_sample_select_func' );

Now, in my cred form I need to replace this:

<div class="form-group">
        <label>Give your answer</label>
	[cred_field field='_wpcf_belongs_sample_id' value='' class='form-control' output='bootstrap']
</div>

with custom shortcode [custom_new_sample_select] and make cred form understand variable newparent as input, using cred_save_data hook?

Any hints please?

Thanks,
Kostas

#569217

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello,

We need to add following code in theme’s functions.php file. When user submits CRED form, it will get sample post ID from our custom dropdown and save it as parent in the submitted post using cred_save_data hook.

add_action('cred_save_data', 'prefix_my_cred_cred_save_data_action', 10, 2);
function prefix_my_cred_cred_save_data_action( $post_id, $form_data ){
    if ($form_data['id']==39){ //do the following code only if the CRED form ID is 39, adjust this to your CRED form ID
        if (!empty($_POST['newparent'])){ //if parent is selected
            $parent_id = $_POST['newparent'];
            add_post_meta($post_id, '_wpcf_belongs_sample_id', $parent_id, true); //save the ID of the sample as parent of the current post
        }
    }
}

==> Replace ‘39’ with your CRED form id.
==> Please go through above code, you might also want to change something else in that (field name, etc).

Thanks

#569247

Nice, thank you Noman!

How am I going to insert my custom shortcode into the form?
Should I insert it using a Generic Field Select?

Thanks again,
Kostas

#569411

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Kostas,

1. You can add your shortcode directly in cred form.

<div class="form-group">
        <label>Give your answer</label>
    [custom_new_sample_select]
</div>
 

2. If you find any issue then please don’t do directly ‘echo’ in your custom shortcode. First save them in some variable and then use return statement as follows.

Following Code:

echo '<select name="newparent" class="form-control" output="bootstrap">';
 
    foreach ($notanswered_sample_list as $post) :
        echo '<option value=' . $post->ID . '>' . $post->post_name . '</option>';
    endforeach;
 
    echo '</select>';
 
    wp_reset_postdata();

Replace with:

$content = '<select name="newparent" class="form-control" output="bootstrap">';
	foreach ($notanswered_sample_list as $post) :
    	$content .='<option value=' . $post->ID . '>' . $post->post_name . '</option>';
	endforeach;
$content .='</select>';
wp_reset_postdata();
return $content;

Thank you

#569480

Excellent, thank you Noman!
The replacing of the 'echo' did the trick!

One last question please:
Is there an easy way to make the newparent field required?
I do not want the users to submit the form with an empty newparent field.

Thanks a lot,
Kostas

#569513

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Glad to help you. To make it required, replace 1st line in above code with this:

 <select name="newparent" class="form-control" output="bootstrap" required> 

If above solution still does not work, then you can try the solutions given on this ticket, there are 2 solutions given here -- one with php and other with JS code:
https://toolset.com/forums/topic/cred-user-field-not-required/

For further questions or issues, please kindly open a new ticket for each. This will help other users with similar problems to find solutions when searching the forum. You can assign those tickets to me directly if you wish to.

Thank you

#569571

Including required in the select tag, worked fine!

Noman, thanks a lot for your support 🙂

I will now close this ticket.

Cheers,
Kostas