• 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
10
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
487
Likes
24
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
10
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
10
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
487
Likes
24
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
10
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.


EDIT: Contrary to what I wrote here, this is NOT what breaks the page. Woody Ad Snippets is NOT the issue – it appears to be an error in either the WooCommerce "Products" template or elsewhere in WordPress.
 
Last edited:

Temyk

Support team
Staff member
Messages
487
Likes
24
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
10
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/Bootstrap page but crashes on a WordPress page using Woody.

UPDATE: If you are reading this subsequent to my original posting, please note that the Woody snippet/shortcode was NOT causing the issue – it is a conflict with the PHP used in WooCommerce, specifically on Product pages. I have gone back to strikethrough incorrect notes based on later findings.


D.J.
 
Last edited:

Temyk

Support team
Staff member
Messages
487
Likes
24
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
487
Likes
24
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
10
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
487
Likes
24
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
10
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
487
Likes
24
Points
18
#14
You need both versions to be activated, for the plugin to work.
 
Messages
10
Likes
0
Points
1
#15
I honestly gave up on this because it's become so frustrating. I tried again over the weekend and today, and I have put about 12 more hours into it so far.

I had started out by using larger, more complicated chunks of PHP and converting them to shortcodes in Woody, and the page (a WooCommerce Product) would break.

So, instead, I decided to test with a simple PHP script:

PHP:
<?php echo "Hello world!"; ?>
In Woody setup, it looks like this:
PHP Echo Test 1.jpg

Correct format? (I think so.)

I then clone a copy of that same snippet, but give it a new name:
PHP Echo Test 2.jpg

Next, after updating them, I place them on my WooCommerce Product page and update:

PHP Echo Test 3.jpg

Then, I click "View Product" and I get this message:

Fatal error: ob_start(): Cannot use output buffering in output buffering display handlers in /htdocs/clickandbuilds/FNCMinistryWP/wp-content/plugins/insert-php/libs/factory/shortcodes/shortcode.class.php on line 286

To clarify, plugins/insert-php is the original Woody Ad Snippets plugin by Will Bontrager (the free version).

So what is line 286 in shortcode.class.php? Here is the raw code:

PHP Echo Test 4.jpg

Does this mean that although my code is good, something in the original Woody plugin's code is breaking the page?

(Deleting the two Woody shortcodes unbreaks the page, by the way.)

Thanks again,

D.J.
 
Last edited:

Temyk

Support team
Staff member
Messages
487
Likes
24
Points
18
#16
Hello.

This is woody's normal job. The point here is that Woocommerce most likely performs output also via the ob_start() function. This is why this error appears.
Are you using ob_start somewhere else?

Try inserting one of these shortcodes into a regular post. Does it work?
 
Messages
10
Likes
0
Points
1
#17
Try inserting one of these shortcodes into a regular post. Does it work?
Yes. I actually stacked four different shortcodes in a regular WordPress page and a post, and they worked flawlessly.

Are you using ob_start somewhere else?
No – not in any code that I have written.

The point here is that Woocommerce most likely performs output also via the ob_start() function.
That appears to be true. I've been thinking for months that this is either (1) a Woody problem, which it clearly isn't; or (2) a problem with my code. It would appear that it is actually a WooCommerce issue.

To close this topic out, I'll note that I tried the same scenario using a couple of PHP shortcodes generated through XYZ's plugin on a Woo Product page, and it also errored out:

Fatal error: ob_start(): Cannot use output buffering in output buffering display handlers in /FNCMinistryWP/wp-content/plugins/insert-php-code-snippet/shortcode-handler.php on line 18
So, Artem, thank you for giving this so much attention – I'm sorry for taking up so much of your time, but I appreciate it very much!

D.J.


P.S. – Unless there is a workaround for this, please mark this topic RESOLVED!
 
Last edited: