0

I have a very weird issue trying to do a simple str_replace in php in a wordpress application.

I am trying to create a path to an image using wordpress functions to get the site path and the resource path. Below are 3 methods that I tried, all giving results that I don't understand.

Original method: (This worked on my own localhost but when in prod, the admin side gives a different $fullpath to the front end side)

$site_url = get_site_url();
$homepath = get_home_path();
$filepath = str_replace($site_url.'/', '', $attachment->image_src_large[0]);
$fullpath = $homepath.$filepath;

Output on Front End was a $fullpath of

string(114) "/usr/www/users/currentuser/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"

But when in Admin End was (Note the double forward slash)

string(114) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image1-2962-1024x684.jpg"

I noticed that on the production server $attachment->image_src_large[0] didn't include the domain as it had on the dev server. So with this in mind, and in an attempt to solve the double slash on the Admin End, I tried numerous methods as detailed below - This is when things started to get a bit weirder:

2.

$site_url = get_site_url();
$homepath = get_home_path();
$filepath = $attachment->image_src_large[0];
$fullpath = str_replace("//", "/", $homepath.$filepath);

//var_dumps Give
//FRONT END
//$attachment->image_src_large[0] - string(89) "/wp-content/uploads/2017/10/image1-2962-1024x684.jpg" 
//$filepath - string(114) "/usr/www/users/currentuser/https:/www.currentdomain.co.za/wp-content/uploads/2017/10/image1-2962-1024x684.jpg" 

//ADMIN (Gave http instead of https)
//$attachment->image_src_large[0] - string(84) "/wp-content/uploads/2017/10/image1-2962-1024x684.jpg" 
//$filepath - string(109) "/usr/www/users/currentuser/http:/currentdomain.co.za/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"

$site_url = get_site_url();
$homepath = get_home_path();
$filepath = str_replace('/', '', $attachment->image_src_large[0]);
$fullpath = $homepath.$filepath;

//var_dumps Give
//$attachment->image_src_large[0] - string(51) "/wp-content/uploads/2017/10/image1-1024x684.jpg"
//$filepath - NULL

3.

$site_url = get_site_url();
$homepath = get_home_path();
$filepath = $attachment->image_src_large[0];
$fullpath = $homepath.$filepath;
$finalpath = str_replace("//", "/", $fullpath);

$fullpath - string(115) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image-1-1024x684.jpg" 
$finalpath - string(114) "/usr/www/users/currentuser/https:/www.currentdomain.co.za/wp-content/uploads/2017/10/image-1-1024x684.jpg"

The weird thing here was the size of the $finalpath string vs $fullpath sting, it looks like the str_replace works but when php reads the string it replaces / with the domain

4. Tried escaping the forward slash to be inserted

...
$finalpath = str_replace("//", "\/", $fullpath);

//$fullpath - string(110) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image-1-1024x684.jpg" 
//$finalpath - string(110) "/usr/www/users/currentuser/http:\/currentdomain.co.za/wp-content/uploads/2017/10/image-1-1024x684.jpg" 

The output that I am looking for is this - string(114) "/usr/www/users/currentuser/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"

I can't figure this one out! Anybody able to help, I would greatly appreciate it!

Bluekable
  • 61
  • 10
  • What is the output of `site_url` and `homepath`? –  Feb 12 '18 at 10:51
  • They were as expected: `site_url - https://currentdomain.co.za` and `homepath - /usr/www/users/currentuser/`. Both were strings. `site_url' was actually not used after what I noticed just before point 2 above. – Bluekable Feb 12 '18 at 12:14
  • What is the output that you want? –  Feb 12 '18 at 12:39
  • The output that I am looking for is this - `string(114) "/usr/www/users/currentuser/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"` – Bluekable Feb 12 '18 at 12:45

2 Answers2

0

As you already have

$homepath = '/usr/www/users/currentuser/' $filepath = /wp-content/uploads/2017/10/image1-2962-1024x684.jpg' (from $attachment->image_src_large[0])

you can do:

$fullpath = substr($homepath, 0, strlen($homepath)-1) . $filepath;
$fullpath = str_replace('https://currentdomain.co.za/', '', $fullpath);
$fullpath = str_replace('//', '/', $fullpath);

By using the substr function, the last / in $homepath is removed (this code is reading from the beginning of the string up until the last character, which is /). This is probably optional as the browser/web server should automatically remove the extra /.

Next, replace the domain name with an empty string and the replace the // with an empty string, specifically in that order.

I hope this solves your problem

  • Thanks a lot for the answer. However I must ask a question and point something out. **Question** - why would i need `$fullpath = str_replace('https://currentdomain.co.za/', '', $fullpath);` if the domain doesn't actually appear in either `$fullpath` or `$filepath`. **Point** - I tried `$fullpath = str_replace('//', '/', $fullpath);` in a few different forms above and none of them worked. See attempt 3 and 4 in my question. Or is there something there that I am missing? – Bluekable Feb 12 '18 at 13:47
0

I figured out a solution! Not that it explains some of the weird str_replace activity that I was seeing, so if anyone can explain that please do.

But to get around the original issue I was experiencing - The line

$filepath = str_replace($site_url.'/', '', $attachment->image_src_large[0]);

was only having an effect on either back or front end but not both because $attachment->image_src_large[0] was different for each. On front end I would get https://... but on back end, for whatever reason, I would get http://....

So I expanded $site_url = get_site_url(); to be:

$site_url = get_site_url();
  if (strpos($site_url, 'https') !== false){
    $ssl_site_url = $site_url;
    $plain_site_url = str_replace("https", "http", $site_url);
    } else {
    $plain_site_url = $site_url;
    $ssl_site_url = str_replace("http", "https", $site_url);
    }

and then did a str_replace() for both strings. Might be overkill but it will work no matter what protocol $attachment->image_src_large[0] turns out to be.

Bluekable
  • 61
  • 10