Skip Navigation

[Resolved] cred forms + conditionals + custom function

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

Problem: I would like to use a custom function in a conditional shortcode inside a CRED form.

Solution: Write your custom function in PHP, then register the function in Toolset > Settings > Frontend Content > Functions inside conditional evaluations. Then you can use the custom function inside a conditional HTML clause.

This support ticket is created 6 years, 6 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.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 3 voices.

Last updated by ericW-5 6 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#878821
cred form 120518.PNG

I am building a job site with candidates and recruiters.

I managed to limit the number of posts submission per role using toolset support former explanations as follows.

1 - I added the following code to functions.php

/*** Count posts of given type, so each user can create post only [desired number of posts] posts */

function cv_post_count() {
$user_post_count = count( get_posts( array( //we get the posts
'post_type' => 'candidature',
'author' => get_current_user_id(),
) ) );
return $user_post_count;
}

function joboffer_post_count() {
$user_post_count = count( get_posts( array( //we get the posts
'post_type' => 'offre-d-emploi',
'author' => get_current_user_id(),
) ) );
return $user_post_count;
}

2 - I added conditionnality to cred forms as follows :

2a - limit post submission for candidates

if user role is candidat_v1 or candidat_premium and if number of posts is lower than 1 then display the form

[wpv-conditional if="(( '[wpv-current-user info='role']' eq 'candidat_v1' ) OR ( '[wpv-current-user info='role']' eq 'candidat_premium' )) AND ( cv_post_count() lt '1' )"]

--> this works fine

2b - limit post submissions for recruiters
if (user role is employeur_v1 and number of posts is lower than 1) or if (user role is employeur premium and number of posts is lower than 2) then display the form

[wpv-conditional if="((( '[wpv-current-user info='role']' eq 'employeur_v1' ) AND ( joboffer_post_count() lt '1' )) OR (( '[wpv-current-user info='role']' eq 'employeur_premium' ) AND ( joboffer_post_count() lt '2' )))”]

--> this does not work, the form is simply never displayed to any role
I suspect there is a problem in this CRED form the closing tag [/wpv-conditional] is not of the right colour...
--> the cred form is here :hidden link

I attached a screenshot of this form but an admin access would be more confortable, tell me if you need one.

Thanks in advance for your help
Eric

#879158

Try to adapt the apostrophes in that condition.

WordPress does not like at all nesting ShortCodes and completely fails when nested ShortCodes hold the same apostrophes as their wrapping ShortCodes.
Toolset makes nesting ShortCodes possible, which is not something usual in WordPress.

Hence, you need to give an extra layer of attention to the syntax here.

I paste below a simplified version of the code, please note how I alter the apostrophes in the ShortCodes.
Either use single within double or double within single.
Never use single withing single or double within double.

[wpv-conditional if="( '[wpv-current-user info="role"]' eq 'administrator' ) AND ( '1' eq '1' ) OR ( '[wpv-current-user info="role"]' eq 'any_role' ) AND ( '1' eq '1' )"]
 The Conditions is true/false
[/wpv-conditional]

Note the particularity of the apostrophes:

'[wpv-current-user info="role"]'

Originally you would write it like this:

"[wpv-current-user info="role"]"

That will fail, though.

Hence, I think after adjusting the apostrophes your condition will work.

#879365

Thanks Beda for your quick answer.
Well noted for the apostrophes, I missed that point.

Nevertheless I still have issues.
I oversimplified the conditions to test my CRED form "Publier_offre_emploi"

1 - I still have this function in functions.php as follows

function joboffer_post_count() {
$user_post_count = count( get_posts( array( //we get the posts
'post_type' => 'offre-d-emploi',
'author' => get_current_user_id(),
) ) );
return $user_post_count;
}

2 - I tested my CRED Form with the following simple condition

[wpv-conditional if="( '[wpv-current-user info="role"]' eq 'employeur_premium' ) "]

--> Perfect : I can see the form when I am logged in as an 'employeur_premium'

3 - Then I tested my CRED Form with the following double condition

[wpv-conditional if=" ( '[wpv-current-user info="role"]' eq 'employeur_premium' ) AND ( joboffer_post_count() lt '2') "]

--> Problem : I cannot see the form when logged in as an 'employeur_premium' who published no joboffer yet... which surely means there is a problem with the function... but I cannot find where the issue is...

Thanks in advance for your help

Eric

#880818

Hi, in order to use a custom function in a conditional like this, you must register the function. Go to Toolset > Settings > Front-end Content > Functions inside conditional evaluations, and you should find the two custom functions listed here. If not, you must enter the name of each function: "joboffer_post_count" and "cv_post_count" without quotation marks.

If that does not work, please add the debug parameter to each conditional and copy + paste the output for me to review.

[wpv-conditional if=" ( '[wpv-current-user info="role"]' eq 'employeur_premium' ) AND ( joboffer_post_count() lt '2') " debug="true"]
#882046

Thanks alot !
All solved - Excellent support as usual.