Page 1 of 1

How to replace a variable with actual url?

Posted: Thu Mar 20, 2014 11:37 pm
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

Re: How to replace a variable with actual url?

Posted: Fri Mar 21, 2014 7:12 am
by Celauran
Store the S3 URL in a variable and call that instead of get_stylesheet_directory_uri()

Re: How to replace a variable with actual url?

Posted: Fri Mar 21, 2014 11:33 pm
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

Re: How to replace a variable with actual url?

Posted: Sat Mar 22, 2014 1:59 am
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

Re: How to replace a variable with actual url?

Posted: Sat Mar 22, 2014 7:52 am
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' ) );

Re: How to replace a variable with actual url?

Posted: Sat Mar 22, 2014 11:51 am
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