Skip Navigation

[Gelöst] using site / global variables – where I do set and change?

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

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

Zuletzt aktualisiert von robertM-17 vor 7 Jahren.

Assistiert von: Luo Yang.

Author
Artikel
#592236

I am trying to:
1) use a variable which has been defined as a global.
2) pass that variable to be used on another page/form

Link to a page where the issue can be seen:
CRED SUCCESS REDIRECT HOOK

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

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

  	//global $working_memap_id;
    	$working_memap_id = $post_id;
         echo $working_memap_id;

    $url = 'my link url';
    	
    return $url;
  }
}

also I have to MAKE A SHORTCODE for access the php variable in wp

$working_memap_id = 0;

add_shortcode( 'working_memap_id', 'working_memap_id_func');
function working_memap_id_func($atts)
{
  global $working_memap_id;
  return $working_memap_id;
}

I expected to see:
When the form jumps to the redirect url, the working_$memap_id variable to be set by the redirect function and available for use in this new page that we've arrived on.

Instead, I got:

Thanks for the support on the previous threads so far from your team.

The shortcode works as expected. On the jump page I've used :
[working_memap_id] to check it's available but it still shows the initial variable value, whatever I set it to (in this case 0)

My question and problem is the scope of the global variable and where to set it..
1) As you can see it's just floating there. Not inside a function or anything. It is just set in a code customisations plugin at the moment (like all the other function) but when I call that function from cred_success_redirect , it's value does not change to what has been set in that function? Do I need to wrap it in another function - if so when to call?

2) how else or where should I set the initial value variable (if at all) so that when redirecting to/loading the new page, it does not cause it to revert to the initial value?

3) or what do I do the ensure the value of the cred_success_redirect function is held and passed over to the new page we are redirected to?

I fear this is php101 for newbies question - and something like use -> instead of = or use " instead of ' but I will ask anyway - we are still out there!

I've have been looking at php resources and threads on this site but to no avail.

Much Thanks in advance

Robert

#592237

I also tried not setting the variable at all initally by commenting out the :

//$working_memap_id = 0;

but that did not seem to work either

#592409

Dear Robert,

It is a custom PHP codes question, you can setup/change/render the global variables with $GLOBALS super global array like

$GLOBALS['working_memap_id'] = 123; // setup/change the global variable value

More help:
versteckter Link

#592851

Luo,

Thanks for reply. Not sure if it's out of the scope of your support. Sorry if so but I do have a couple of issues with scope myself, which I hope someone can help with.

GLOBAL VAR - NOT OK
- I've been trying that $GLOBALS method you suggest as well as trying it with the global keyword within the cred_redirect_function to bring the value into the function. (my code above it's commented out and that's actually a mistake, but it wasn't always). Neither method - $GLOBALS or global keyword (seem to pass the variable to the next form via the redirect.
I've also wrapped it in an isset() check to get see if it could just let the redirect set it:

 
if (!isset($working_memap)){
$working_memap='';
}	

(I changed the var name in case of conflict with the shortcode name - just in case that was a problem)

But that isset() check still does not seem to pass the new $working_memap value out of the cred_success_redirect.

SHORTCODE - OK
Thanks to Christian's advice, I can display the value of the variable using a shortcode and I can see it always goes back to the original value on the directed page. How do I get it to keep the value of the post_id/$working_memap_id so that i can continue to be worked with across various forms?

CRED REDIRECT - PARTLY OK
Thanks to Beda's advice
https://toolset.com/forums/topic/passing-new-cpt-idslug-to-another-form/
I've been getting the forms to redirect and the ID of the new post is passed on but it doesn't work as expected. and the url looks like this.
versteckter Link

It incorporates the referral form, does that affect it?
because I would like the new page which contains an EDIT form to pick up the newly created CPT entry for editing. But it doesn't

==At the moment I am trying this through variables, should it be done through fields/hidden fields/values on the form/page instead?
Really, at this stage all I want is :

1) create a new CPT on one page/form
2) jump to next page/form to edit the same cpt - with the title/name displayed to show what is being edited.

I've not been able to get that going ..

Very Grateful Thanks

#592990

Ok thanks for this.
I've got Beda's original support guidance working now.
https://toolset.com/forums/topic/passing-new-cpt-idslug-to-another-form/
I'd made a mistake in how the shortcode on the receiving form was written.

Can work with that for now.
Apologies