Skip Navigation

[Gelöst] Using PHP variables in a shortcode

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I would like to use a PHP variable in a shortcode attribute.

Solution: You can't use PHP variables directly in your shortcode attributes. Instead, you could create a custom shortcode that returns the value of that variable, and place that shortcode instead of the variable in your attribute.

$working_memap_id = 'some-post-slug';
 
add_shortcode( 'global_memap_id', 'global_memap_id_func');
function global_memap_id_func($atts)
{
  global $working_memap_id;
  return $working_memap_id;
}
[cred form form="properformname" post="[global_memap_id]"]

Register the name of the shortcode in Toolset > Settings > Frontend content > 3rd party shortcode arguments.

Relevant Documentation: https://codex.wordpress.org/Shortcode_API
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/

This support ticket is created vor 7 Jahren. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 2 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von robertM-17 vor 7 Jahren.

Assistiert von: Christian Cox.

Author
Artikel
#591225

Tell us what you are trying to do?

ONE)
use a global variable, set elsewhere in the site theme. In this case to determine with post to edit, after being referred from another page / form

Is there any documentation that you are following?
https://toolset.com/forums/topic/passing-new-cpt-idslug-to-another-form/
https://toolset.com/documentation/user-guides/creating-cred-forms/

I've tried:

add_filter('cred_success_redirect', 'mm101_redirect',10,3);

$working_memap_id="no working memap";

function mm101_redirect($url, $post_id, $form_data)
{
  if ($form_data['id']==3356){

         $url = '<em><u>versteckter Link</u></em>'.$post_id;
    	global $working_memap_id;
         $working_memap_id=$post_id;
    return $url;
  }
}

I'd like the variable $working_memap_id to be available so I could use it in a shortcode like
[cred form form="properformname!" post=$working_memap_id]

Following Beda's guidance and documentation, as well as threads on Toolset support forums discussing - Global variables. But they don't help me to understand.

TWO)
Also, I would like some clarification please about this, from your documentation on the cred_success_redirect hook...
https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect

add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $post_id, $form_data)
{
    if ($form_data['id']==12)
        return '<em><u>versteckter Link</u></em>';
    return $url;
}
//This filter can also be applied for a specific form, using the form ID:
add_filter('cred_success_redirect_12', 'custom_redirect_for_form_with_id_12', 10, 3);
function custom_redirect_for_form_with_id_12( $url, $post_id, $form_data )
{
    $newurl = '<em><u>versteckter Link</u></em>';
    return $newurl;
}

I think I have the If() statement working based on form ID as in my code example above. The second part / alternative way to test form the form, I am not clear about.
Does using the
cred_success_redirect_12 automatically check for form id-12 without having to use the if() statement?
Could I therefore use

cred_success_redirect_3356

and achieve the same result as
...
if($form_data['id]==3356)
...

Thanks in advance
Robert

#591359

I'd like the variable $working_memap_id to be available so I could use it in a shortcode like
[cred form form="properformname!" post=$working_memap_id]

You cannot use a PHP variable directly in a CRED form shortcode like this. Instead, you should create a custom shortcode that returns the value of the global variable, and use that shortcode instead. Be sure to register your shortcode in Toolset > Settings > Frontend content > 3rd party shortcode arguments.

$working_memap_id = 'some-post-slug';

add_shortcode( 'global_memap_id', 'global_memap_id_func');
function global_memap_id_func($atts)
{
  global $working_memap_id;
  return $working_memap_id;
}
[cred form form="properformname" post="[global_memap_id]"]

More info on these concepts:
https://codex.wordpress.org/Shortcode_API
https://toolset.com/documentation/user-guides/shortcodes-within-shortcodes/

Does using the 'cred_success_redirect_12' automatically check for form id-12 without having to use the if() statement?
Could I therefore use 'cred_success_redirect_3356' and achieve the same result as 'if($form_data['id]==3356)'
Yes, correct on both accounts. You could use the form ID in the action name instead of the if statement, if you'd like. The if statement is more flexible, so you can use the same redirect code for more than one form. The form ID in the action name approach requires a bit less code, but is limited to only one form.

#591617

Christian
Thanks for the support and clarification.
I get that now about the function with the number or the if () statement. It's not overly clear in the docs (to a php simpleton)

And for the shortcode-making code.
Code really is poetry. I have been a little fearful of it but that's reassuringly simple and getting toward some sort of code haiku.
It works beautifully for displaying that variable.

I'll soon be testing with forms but am also trying to resolve that CRED issue - as on another ticket you are supporting.

Thanks again.

Robert