Hello,
I’m creating some functions and I’m trying to access a variable within a function
In the following function I would like to make $latitude available and ready to be used in another function on the same page called my_gallery_shortcode().
function grab_exif_data() {
$a = 12;
$b = 53;
$imgmeta = wp_get_attachment_metadata($id);
$latitude = $imgmeta['image_meta']['latitude'];
$longitude = $imgmeta['image_meta']['longitude'];
echo $longitude;
echo $a;
echo $b;}
Like I mentioned I would like to use the $latitude variable in the following function
function my_gallery_shortcode() {
do something;
echo $latitude;
}
I could use the function grab_exif_data() but that would echo all the variables in in the function my_gallery().
I only need the variable $latitude to be available in the function my_gallery().
Probably an easy one for you guys but I couldn’t find a solution for this problem.
thanks
You could assign both the latitude and longitude to an array, and return that array from the function.
That way you will have both of them accessible, should you need them later on.
function grab_exif_data() {
...
$exifData = array(
'latitude' => $imgmeta['image_meta']['latitude'],
'longitude' => $imgmeta['image_meta']['longitude'],
);
...
return $exifData;
}
$exifData = grab_exif_data();
echo $exifData['longitude'];
thanks I’ll try your tip…
There could be two ways as per your requirement:
- Using global keyword.
$long = null;
$lat = null;
function grab_exif_data(){
global $long, $lat;
$long = 54.3432423;
$lat = 54.3432345;
}
function my_gallery_shortcode() {
global $long, $lat;
//do something;
//echo $lat;
}
grab_exif_data();
echo "Long: " . $long . " Lat : " . $lat;
my_gallery_shortcode();
- Passing as parameter to second function returned by first function:
function grab_exif_data(){
$return = array('long' => 54.3432423, 'lat' => 54.3432345);
return $return;
}
function my_gallery_shortcode($lat) {
//do something;
//echo $lat;
}
$data = grab_exif_data();
my_gallery_shortcode($data['lat']);
I would recommend the second method.
Thanks I’ll give the second one a shot…