• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

RESOLVED Using Local Variables In Woody Ad Snippets

Jaxxon5

New member
Messages
7
Likes
0
Points
1
#1
Hello!

I'm converting an HTML-based website to WordPress, but many of the HTML pages incorporate PHP to duplicate text that is common across the website.

Here's my question: how do you combine a series of variables such as this:
PHP:
<?php

    $state = "California";

    $city = "Los Angeles";

?>
Into a second chunk of text-based code such as this:
HTML:
<h2>Legal Requirements For <?php echo $state; ?></h2>

<p>Wondering how to register in <?php echo $state; ?></p>
We're trying to create 50 different pages – one for each of the 50 states in the USA – without having to write every line of code for each page.

To clarify, the main text on the pages for California and Texas will be the same, except for the variables – on California, it would have
Code:
 $state = "California";
while on the Texas page, the variable would be
Code:
 $state = "Texas";
I've tried doing the shortcode as PHP on both, as TEXT in both, in PHP on one and TEXT on the other (and vice versa). Nothing has worked so far.

This should be easy – I've done this on dozens of HTML and Bootstrap websites over the years, but WordPress is always a roadblock for me on this.

Any clues, hints or solutions would be greatly appreciated.

Thank you!

D.J.
 
Last edited:

Temyk

Support team
Staff member
Messages
263
Likes
19
Points
18
#2
Hello.

If I understand you correctly, you want to create a snippet that will store all the names of the States?
Use global when you declare a variable and before you use it.
PHP:
<?php
    global $state = "California";
    global $city = "Los Angeles";
?>
PHP:
<?php
global $state;
global $city;
?>
<h2>Legal Requirements For <?php echo $state; ?></h2>
<p>Wondering how to register in <?php echo $state; ?></p>
 

Jaxxon5

New member
Messages
7
Likes
0
Points
1
#3
Artem,

Thank you for your reply!

I think I'm explaining this wrong, and I appreciate your patience.

If I understand you correctly, you want to create a snippet that will store all the names of the States?
Actually, what I'm trying to do is create 50 different pages – one for each state. Each page would have specific information for that state (for example, Texas) but since a chunk of text is common to all 50 pages, I want to re-use it for all of the pages, but pull the variable via PHP.

There would be two snippets:

1. The common chunk of text that will be re-used; and

2. The bit of PHP that defines the variables for that state:

Code:
<?php
    $state = "California";
    $city = "Los Angeles";
?>
This code (above) would be unique to the California page. The other states (Texas, Michigan, Florida, etc.) would each have their own specific snippet.

(This currently works perfectly on our HTML/Bootstrap website; it's a real hassle in WordPress!)

Thanks again for your generous assistance, and have a great weekend!

D.J.
 

Jaxxon5

New member
Messages
7
Likes
0
Points
1
#4
Artem,

I noticed in another forum thread that you mentioned:

All variables that you use between snippets must be declared as global in both snippets.
Add this line to the beginning of both snippets:
PHP:
global $myDB;

global $start;

global $end;
Since I am using two snippets on the same page, inline like this–

[wbcr_php_snippet id="3014"]
[wbcr_php_snippet id="3023"]

–do I need to add a similar line to each of my original PHP snippets?

I did try to format my two PHP snippets as you showed in your original reply (#2 above) but it broke the page. I'm sure it was an error in my code as I wrote it.

Thanks again!

D.J.
 

Temyk

Support team
Staff member
Messages
263
Likes
19
Points
18
#5
I did try to format my two PHP snippets as you showed in your original reply (#2 above) but it broke the page. I'm sure it was an error in my code as I wrote it.
Add code to snippet without <?php and ?>
PHP:
global $state = "California";
global $city = "Los Angeles";
–do I need to add a similar line to each of my original PHP snippets?
yes
 

Jaxxon5

New member
Messages
7
Likes
0
Points
1
#6
Artem,

Thank you for your reply, and I apologize for being a pest!

You wrote:

Add code to snippet without <?php and ?>
How do I do that in a PHP snippet? When I do this, Woody automatically adds the opening <?php (but not the closing ?>) which breaks the page.

Thanks again!

D.J.
 

Temyk

Support team
Staff member
Messages
263
Likes
19
Points
18
#7
Just write the code in the text box, woody will automatically substitute <php at the beginning and ?> at the end.
If you still have errors, please send me screenshots of your snippets(a few for understanding)
 

Jaxxon5

New member
Messages
7
Likes
0
Points
1
#8
Artem,

Here's a screenshot of the "local" PHP snippet for Washington State – it appears in the body of the Washington State page, before the Woody snippet that merges these variables into a common set of text:

PHP Snippet (Washington).png

Note that in this example Woody automatically adds the front <php but NOT the closing ?> tag – which crashes the page.

However, if I add the closing ?> tag ... it also crashes the page.
PHP Snippet (Page Crash).png

But if I remove the first "local" snippet, the page is okay ... except the "local" variables disappear.

Note that the "global" PHP snippet also passes through the raw <?php echo $state; ?> code to the page, but simply skips the other instances of $state – there are four of them in the screenshot, none of which appear.

Again, this works perfectly on a straight HTML page, but crashes on a WordPress page using Woody.

ADD: Am I using the wrong format(s) on my snippet(s), perhaps? I know that my snippet that defines the "local" variables should be a PHP snippet, but should my "global" snippet be an HTML (or Universal) snippet, rather than PHP?

Thanks again!

D.J.
 
Last edited:

Temyk

Support team
Staff member
Messages
263
Likes
19
Points
18
#9
Can you give access to your website? So I can see and check your snippets. You can send in private messages
 

Temyk

Support team
Staff member
Messages
263
Likes
19
Points
18
#10
I fixed it. You had to declare a global variable first and then assign a value to it. This is my mistake.
I added the correct code to the Washington State snippet (PHP).
PHP:
global $state;
global $keycity1;
global $keycity2;
global $keycity3;

$state = "Washington";
$keycity1 = "Seattle-Tacoma";
$keycity2 = "Spokane";
$keycity3 = "Vancouver";
 

Jaxxon5

New member
Messages
7
Likes
0
Points
1
#11
Artem,

That worked!:geek:

I see that you also wrapped the snippets in <p> </p>. Did that happen automatically, or does that help the code work?

Either way, I appreciate your help. This does exactly what we needed!

I'm paying for the PRO version today. You went above and beyond to help me with this, and I appreciate it!

D.J.
 

Temyk

Support team
Staff member
Messages
263
Likes
19
Points
18
#12
I see that you also wrapped the snippets in <p> </p>. Did that happen automatically, or does that help the code work?
This happens automatically in the post's text editor in the Visual tab. You can remove this tag if you don't need it.
 

Jaxxon5

New member
Messages
7
Likes
0
Points
1
#13
Artem,

One last question on this topic: we've purchased the PRO version of Woody, installed and activated it.

Now that it's running, we've got two iterations of the plugin installed:

Woody Snippets (2 Versions Running).png

Do we keep both versions active, or can we deactivate and delete the free version without any issues (such as having it also delete the snippets we've got running)?

Thank you!

D.J.
 

Temyk

Support team
Staff member
Messages
263
Likes
19
Points
18
#14
You need both versions to be activated, for the plugin to work.