I am working on a modification of a class from the NextGen Gallery Wordpress plugin. Ultimately I'm trying to build a URL to an image by assembling the necessary bits that NGG has stored in the database.
I've gotten to the point where the original code performs the SQL query and gives an array then extracts certain values from that array. The array contains all the values I need, but I can only seem to get SOME of the values and I cannot figure out why.
Here is my modified code as it currently stands:
// Class copied from ngg plugin, change class name from 'nggWidget' to 'nggHeaderBG'
class nggHeaderBG extends WP_Widget {
function widget( $args, $instance ) {
global $wpdb;
$items = $instance['items'];
$exclude = $instance['exclude'];
$list = $instance['list'];
$exclude_list = "AND t.gid IN ($list)";
$imageList = $wpdb->get_results("SELECT t.*, tt.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE tt.exclude != 1 $exclude_list ORDER by rand() limit {$items}");
foreach($imageList as $image) {
// get the URL constructor
$theimage = new nggImage($image);
// Just create the image URL
$out .= ''.home_url().'/'.$theimage->path.'/'.$theimage->filename;
echo $out;
}
}
In the code above, $theimage->filename
works, but $theimage->path
does not. A var_dump($theimage)
produces the following array (it's long, so I'll store it in a code snippet):
object(nggImage)#3716 (2) {
["_ngiw"]=>
object(C_Image_Wrapper)#3717 (8) {
["_cache"]=>
array(34) {
["alttext"]=>
string(9) "homepage3"
["author"]=>
string(2) "10"
["description"]=>
string(1) " "
["errmsg"]=>
string(0) ""
["error"]=>
bool(false)
["exclude"]=>
string(1) "0"
["filename"]=>
string(13) "homepage3.gif"
["galdesc"]=>
string(72) "For Home Landing Page Only.
Must be 880 pixels wide by 350 pixels high."
["galleryid"]=>
string(1) "2"
["gid"]=>
string(1) "2"
["hidden"]=>
bool(false)
["href"]=>
string(0) ""
["imagePath"]=>
string(0) ""
["imageURL"]=>
string(0) ""
["image_slug"]=>
string(9) "homepage3"
["imagedate"]=>
string(19) "2014-10-01 16:03:54"
["meta_data"]=>
array(17) {
["width"]=>
int(880)
["height"]=>
int(350)
["full"]=>
array(2) {
["width"]=>
int(880)
["height"]=>
int(350)
}
["thumbnail"]=>
array(4) {
["width"]=>
int(350)
["height"]=>
int(139)
["filename"]=>
string(20) "thumbs_homepage3.gif"
["generated"]=>
string(21) "0.25234600 1412179434"
}
["aperture"]=>
bool(false)
["credit"]=>
bool(false)
["camera"]=>
bool(false)
["caption"]=>
bool(false)
["created_timestamp"]=>
bool(false)
["copyright"]=>
bool(false)
["focal_length"]=>
bool(false)
["iso"]=>
bool(false)
["shutter_speed"]=>
bool(false)
["flash"]=>
bool(false)
["title"]=>
bool(false)
["keywords"]=>
bool(false)
["saved"]=>
bool(true)
}
["name"]=>
string(14) "second_gallery"
["pageid"]=>
string(1) "0"
["path"]=>
string(31) "wp-content/gallery/home_headers"
["permalink"]=>
string(0) ""
["pid"]=>
string(3) "411"
["post_id"]=>
string(1) "0"
["previewpic"]=>
string(3) "403"
["slug"]=>
string(12) "home_headers"
["sortorder"]=>
string(1) "0"
["style"]=>
string(0) ""
["tags"]=>
string(0) ""
["thumbFolder"]=>
string(8) "/thumbs/"
["thumbPath"]=>
string(0) ""
["thumbPrefix"]=>
string(7) "thumbs_"
["thumbURL"]=>
string(0) ""
["thumbcode"]=>
string(0) ""
["title"]=>
string(12) "Home_Headers"
}
["_settings"]=>
NULL
["_storage"]=>
NULL
["_galleries"]=>
NULL
["_orig_image"]=>
object(stdClass)#3715 (20) {
["gid"]=>
string(1) "2"
["name"]=>
string(14) "second_gallery"
["slug"]=>
string(12) "home_headers"
["path"]=>
string(31) "wp-content/gallery/home_headers"
["title"]=>
string(12) "Home_Headers"
["galdesc"]=>
string(72) "For Home Landing Page Only.
Must be 880 pixels wide by 350 pixels high."
["pageid"]=>
string(1) "0"
["previewpic"]=>
string(3) "403"
["author"]=>
string(2) "10"
["pid"]=>
string(3) "411"
["image_slug"]=>
string(9) "homepage3"
["post_id"]=>
string(1) "0"
["galleryid"]=>
string(1) "2"
["filename"]=>
string(13) "homepage3.gif"
["description"]=>
string(1) " "
["alttext"]=>
string(9) "homepage3"
["imagedate"]=>
string(19) "2014-10-01 16:03:54"
["exclude"]=>
string(1) "0"
["sortorder"]=>
string(1) "0"
["meta_data"]=>
array(17) {
["width"]=>
int(880)
["height"]=>
int(350)
["full"]=>
array(2) {
["width"]=>
int(880)
["height"]=>
int(350)
}
["thumbnail"]=>
array(4) {
["width"]=>
int(350)
["height"]=>
int(139)
["filename"]=>
string(20) "thumbs_homepage3.gif"
["generated"]=>
string(21) "0.25234600 1412179434"
}
["aperture"]=>
bool(false)
["credit"]=>
bool(false)
["camera"]=>
bool(false)
["caption"]=>
bool(false)
["created_timestamp"]=>
bool(false)
["copyright"]=>
bool(false)
["focal_length"]=>
bool(false)
["iso"]=>
bool(false)
["shutter_speed"]=>
bool(false)
["flash"]=>
bool(false)
["title"]=>
bool(false)
["keywords"]=>
bool(false)
["saved"]=>
bool(true)
}
}
["_orig_image_id"]=>
string(3) "411"
["_cache_overrides"]=>
NULL
["_legacy"]=>
bool(true)
}
["_propogate"]=>
bool(true)
}
I can get some variables, but not others. In addition to filename, for example, I can get the 'alttext', the 'pid', the 'imagedate', the 'image_slug', 'thumbFolder', 'thumbPrefix', for example. Those all work.
Others, like 'path' and 'slug', either of which I could use to build my URL, always come back with the value 'second_gallery' which is the 'name' value. It seems to be the default fallback for any value that fails to be extracted. I've even tried $theimage->_orig_image->path
, but that produces nothing. Using the square bracket approach, i.e. $theimage['path']
just causes the entire site to mess up.
Can anyone explain why some values can be gotten, but other cannot? And, more importantly, how can I get the values I want (mainly 'path') out of this particular array?
Thanks.
its a mix of arrays and objects but ignoring the cache array this is probably what you are looking for:
$path= $theimage->_ngiw->_orig_image->path;