0

I'm having a hard time with PHP formatting. :P In a function I have these strings and $cache1 path is output properly (wordpress absolute upload path + /json/dailymile/BLABLA.json

$url1 = "http://api.dailymile.com/people/XXXXX/entries.json";
$cache1 = $upload_dir['basedir'].'/json/dailymile/'.sha1($url1).'.json';

Then I have a SIMILAR (if not the same) case but only God knows why, $cache2 returns just /json/instagram/BLABLA.json without wordpress upload path before.

$url2 = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token='.$token.'&count='.$count;
$cache2 = $upload_dir['basedir'].'/json/instagram/'.sha1($url2).'.json';

These two snippets are in the same file and the top of this file I have <?php $upload_dir = wp_upload_dir(); ?>.

Why the second snippet doesn't return the WP upload dir before /json/instagram/BLABLA.json ?

4
  • We might need to see a bit more code. :) Commented Dec 29, 2013 at 15:05
  • Try inserting var_dump($upload_dir); right before the second snippet starts. Commented Dec 29, 2013 at 15:05
  • @ninetwozero added the whole function. ComFreek: ok, trying right now Commented Dec 29, 2013 at 15:12
  • @ComFreek This is the output: pastebin.com/raw.php?i=9PmCQq9d Commented Dec 29, 2013 at 15:17

1 Answer 1

1

$upload_dir is outside of the scope when using your code within a function. Using the global keyword you can import the variable from the global scope into the current one. Like this:

<?php
function get_instagram(...) {
    global $upload_dir;
    ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

While this works, one should rather pass $upload_dir as a parameter.
It depends on the use case and the quality of code you want to achieve. In this case it looks like a "quick hack", we have a function with plenty parameters already. You would probably first want to get rid of all the parameters by wrapping it in a class before you add yet another parameter.
But why I have other functions in the same file and instead for them it works also if it's not inside the function..?
Maybe in the other functions $upload_dir is defined before being used?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.