This code works fine. I am able to get a page of populated JSON that is written to a cache file:
$test_tag = "afro";
$page = 1;
$images = array();
$tags = array();
$imagetype = 'Recent Cuts';
$per_page = 60;
$orderby_view = FALSE;
$tags2 = $test_tag;
$tags = explode(',', $test_tag);
if( count($tags) == 1 && strlen($tags[0]) == 0 ) $tags = array();
$tag_url = urlencode($tags2);
$cachename = dirname( __FILE__ ) . '/cache-fp/' . $imagetype . '-' . $per_page . '-' . $page . '-' . ($orderby_view ? 'by_view' : 'by_date') . $tag_url . '.json';
$detailurl = get_option('image_detail_url');
$detailurl .= (strstr($detailurl, '?') === FALSE ? '?' : '&');
$json = array();
$images = array();
$posts = get_pix($imagetype, array('per_page' => $per_page, 'page' => $page, 'tags' => $tags), $orderby_view);
foreach( $posts['attachments'] as $ii => $post ) {
$ta = array();
$meta = array();
$imagesrclight2 = array();
// BWP - Theater mode
$ta['detail_url'] = $detailurl . 'uid=' . $post->post_author . '&img_id=' . $post->ID . '&theater';
$meta = get_post_meta(get_the_ID(), 'image_tag', false);
$ta['image_tags'] = implode(' ', $meta);
$ta['attachment_image'] = wp_get_attachment_image($image->ID, 'thumbnail');
$imagesrclight2 = wp_get_attachment_image_src($image->ID, array(150, 150));
$ta['attachment_image_src'] = rawurlencode($imagesrclight2[0]);
$images[] = $ta;
}
file_put_contents($cachename, json_encode($images));
BUT when I enclose this code in a function, the JSON response is written to file as before, but it is just an empty skeleton with no data. All I am doing is enclosing the above code inside a new function, passing in the top $test_tag variable and executing the function. I also add an echo() which verifies that $test_tag is passed in. For some reason, it appears that the get_pix() and/or get_option() functions are failing to get the data even though $test_tag is the same in both tests.
$test_tag = "afro";
newFunction($test_tag);
function newFunction($test_tag) {
$page = 1;
$images = array();
$tags = array();
$imagetype = 'Recent Cuts';
$per_page = 60;
$orderby_view = FALSE;
echo $test_tag; //this works, so var is passed in properly
$tags2 = $test_tag;
$tags = explode(',', $test_tag);
if( count($tags) == 1 && strlen($tags[0]) == 0 ) $tags = array();
$tag_url = urlencode($tags2);
$cachename = dirname( __FILE__ ) . '/cache-fp/' . $imagetype . '-' . $per_page . '-' . $page . '-' . ($orderby_view ? 'by_view' : 'by_date') . $tag_url . '.json';
$detailurl = get_option('image_detail_url');
$detailurl .= (strstr($detailurl, '?') === FALSE ? '?' : '&');
$json = array();
$images = array();
$posts = get_pix($imagetype, array('per_page' => $per_page, 'page' => $page, 'tags' => $tags), $orderby_view);
foreach( $posts['attachments'] as $ii => $post ) {
$ta = array();
$meta = array();
$imagesrclight2 = array();
// BWP - Theater mode
$ta['detail_url'] = $detailurl . 'uid=' . $post->post_author . '&img_id=' . $post->ID . '&theater';
$meta = get_post_meta(get_the_ID(), 'image_tag', false);
$ta['image_tags'] = implode(' ', $meta);
$ta['attachment_image'] = wp_get_attachment_image($image->ID, 'thumbnail');
$imagesrclight2 = wp_get_attachment_image_src($image->ID, array(150, 150));
$ta['attachment_image_src'] = rawurlencode($imagesrclight2[0]);
$images[] = $ta;
}
file_put_contents($cachename, json_encode($images));
}
I really hope I am missing something simple. I can only suspect that inside newFunction(), $test_tag is somehow treated differently even though echo() proves it has the same value.
I figured it out. The wordpress function get_the_ID() would not work inside of the function, returning a null value. Instead I used $post->ID, which is identical, and it worked. I don’t like WordPress.