How to replace a variable with actual url?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yolky
Forum Newbie
Posts: 4
Joined: Thu Mar 20, 2014 9:56 pm

How to replace a variable with actual url?

Post by yolky »

On my website I have a nested IF statement where I want to replace the variable and last part of the url with the real url because the images are on Amazon S3 and not the website.

I.E. https://s3.amazonaws.com/tqw/london.jpg https://s3.amazonaws.com/tqw/forest-110900a_7.jpg


if ( is_page('city-quiz') ) :

wp_localize_script( 'agency-pro-backstretch-set', 'BackStretchImg', array( 'src' => get_stylesheet_directory_uri() .'/images/london.jpg' ) );

elseif ( is_page('travel-destination-ideas-quizzes') ) :

wp_localize_script( 'agency-pro-backstretch-set', 'BackStretchImg', array( 'src' => get_stylesheet_directory_uri() .'/images/forest-110900a_7.jpg' ) );

elseif ( is_page('picture-quiz') ) :

...

How do I change the above example statement to do that?

Thanks,

Charles
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to replace a variable with actual url?

Post by Celauran »

Store the S3 URL in a variable and call that instead of get_stylesheet_directory_uri()
yolky
Forum Newbie
Posts: 4
Joined: Thu Mar 20, 2014 9:56 pm

Re: How to replace a variable with actual url?

Post by yolky »

Thanks.

That should work fine.
I hadn't thought of that.
I just thought replacing the variable with the actual url would be straight forward and I could see the url in the statement instead of setting it elsewhere.

Charles
yolky
Forum Newbie
Posts: 4
Joined: Thu Mar 20, 2014 9:56 pm

Re: How to replace a variable with actual url?

Post by yolky »

Hi Celauran,

I tried this as a constant and it didn't work. It didn't blow up, but did not show the image.
I don't know php, but I assume a constant should work the same in this case.

at the beginning of the function:

define( 'S3_URL_CITY', 'https://s3.amazonaws.com/tqw/london.jpg' );


in the IF statement:

if ( is_page('city-quiz') ) :

wp_localize_script( 'agency-pro-backstretch-set', 'BackStretchImg', 'S3_URL_CITY' );




also tried this and same results, no background image showing


at the beginning of the function:

define( 'S3_URL', 'https://s3.amazonaws.com/tqw' );


in the IF statement:

if ( is_page('city-quiz') ) :

wp_localize_script( 'agency-pro-backstretch-set', 'BackStretchImg', 'S3_URL' .'/london.jpg' );


Any idea why these didn't work?

Charles
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to replace a variable with actual url?

Post by Celauran »

From your first code sample, it looks like the third argument is meant to be an array.

Does this work?

Code: Select all

define( 'S3_URL', 'https://s3.amazonaws.com/tqw/' );
...
wp_localize_script( 'agency-pro-backstretch-set', 'BackStretchImg', array( 'src' => S3_URL . 'london.jpg' ) );
yolky
Forum Newbie
Posts: 4
Joined: Thu Mar 20, 2014 9:56 pm

Re: How to replace a variable with actual url?

Post by yolky »

That worked.

I'm not familiar with the structure of php but I guess somewhere in the code wp_localize_script was defined with the third parm to be an array.

When I retired from programming I tried to keep away from web programming. After about 70 or more websites of mine this was the first time I've had to actually delve into the code of one of them.

Although I didn't program in php it seems that I've forgotten a lot.

Thanks much.

I appreciate your help. :)

Charles
Post Reply