Skip Navigation

[Resolved] Unable to show cred user form from shortcode

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

Problem:

Adding the Cred shortcode to show the form does not work in Page Builder

Solution:

To make sure that there is no conflict with the page builder, change the shortcode from something like:

[cred_form form="123"]

To this:

{!{cred_form form="123"}!}

Replace [ with {!{ and ] with }!}

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

This topic contains 4 replies, has 2 voices.

Last updated by himanshuS 2 years, 11 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2285341

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

#2285421

Christopher Amirian
Supporter

Languages: English (English )

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.

#2286069

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.
#2286871

Christopher Amirian
Supporter

Languages: English (English )

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.

#2290067

My issue is resolved now. Thank you!