I am trying to show a different CRED form based on the user role with the following code -
<?php
$user_id = get_current_user_id( );
if($user_id != null) {
$user_meta=get_userdata($user_id);
$user_roles=$user_meta->roles;
$output = 0;
if (in_array("administrator", $user_roles))
{
$output = '[cred_form form="37467"]';
}
if (in_array("subscriber", $user_roles))
{
$output = '[cred_form form="37467"]';
}
if (in_array("member", $user_roles))
{
$output = '[cred_form form="37467"]';
}
}
else {
$output = '[cred_form form="37260"]';
}
echo $output;
?>
However, the form does not output as intended. I tried to do this by manually inserting the form with CRED shortcode - [cred_form form="120"] by reading this - https://toolset.com/documentation/programmer-reference/forms/cred-shortcodes/#cred_form
But nothing is getting rendered. What could I be missing?
Here is a video - hidden link
Hi there,
The code that you have added seems to be ok, the problem I see is that when that code is run. From the video, I see that you use a third party extension to add the PHP code, but where do you say to run that code?
In the Elementor you just added the shortcode for the form. I suggest that you do as follows:
- Go to WordPress Dashboard > Toolset > Custom Code.
- Add a new custom code there and activate it.
- Add the code below: (You can add it after // Put the code of your snippet below this comment.)
add_shortcode( 'selective-froms', 'chr_selective_forms' );
function chr_selective_forms() {
$user_id = get_current_user_id( );
if($user_id != null) {
$user_meta=get_userdata($user_id);
$user_roles=$user_meta->roles;
$output = 0;
if (in_array("administrator", $user_roles))
{
$output = '[cred_form form="37467"]';
}
if (in_array("subscriber", $user_roles))
{
$output = '[cred_form form="37467"]';
}
if (in_array("member", $user_roles))
{
$output = '[cred_form form="37467"]';
}
}
else {
$output = '[cred_form form="37260"]';
}
echo do_shortcode( $output );
}
- Go to the page where you want to show the form with Elementor.
- Add this shortcode there: [selective-forms]
Description of the code:
I used your code, the only change is that I eclosed the code inside add_shortcode function which you can use to add your custom shortcode. I named it "selective-forms". You can name it whatever you want. For more info:
https://developer.wordpress.org/reference/functions/add_shortcode/
The other change that I have done is when you use the $output variable. If you want a shortcode to run inside PHP you need to use the do_shortcode function and after that echo it. For more information:
https://developer.wordpress.org/reference/functions/do_shortcode/
Now you have a custom shortcode that you can use wherever you want, with the criteria that you added in the code.
Thanks.
Chris,
I think we have a fundamental issue that the form does not render by using the cred_form shortcode manually in the page builder. See here - hidden link
If that does not happen, all the other code is irrelevant. Am I missing something while loading a form shortcode in a page builder like elementor?
You have this language in the doc but I don't fully understand it.
When inserting Toolset forms into a page builder design you must use compatibility syntax for Toolset Forms shortcodes:
If you don’t do this the shortcodes may break the backend of the post edit screen while the front-end will still work.
Hi there,
The square brackets are the standard format for shortcodes, as you know.
We introduced the alternative format with curly braces for adding Toolset content inside page builders because there could arise problems with the shortcodes being parsed properly, and we added a mechanism to handle that.
Square brackets mostly work fine, and if you don't encounter any problems I wouldn't change them.
If you do encounter problems using normal shortcodes, you have the alternative available.
For your case it will be something like this:
{!{cred_form form="37260"}!}
So basically you switch "[" with "{!{" and "]" with "}!}"
Thanks.
My issue is resolved now. Thank you!