Alright, this will need a long explanation. Sorry if it's too long:
/////////////// INTRODUCTION ///////////////
There doesn't seem to be much information around about using opendir()
within Wordpress and the way it should be implemented. However, after a couple hours of research, trial and error, I got it working on a custom page template.
I need it to fetch game thumbnail images from folders and output them as a grid, divided by categories (each folder is a category).
/////////////// MY APPROACH ///////////////
I created a function in functions.php
that opens and reads a folder using the following relative path:
"wp-content/games/".$category."/"
Where $category
is the argument of the function (defined by the user).
This is how my function looks like now:
function fetch_game_images($category){
//$category = 'featured';
$imageFiles = array();
$extensions = array('.JPG','.PNG');
$directory = "wp-content/games/".$category."/";
$openDir = opendir($directory);
while ($imgFile = readdir($openDir))
{
if ($imgFile <> '.' and $imgFile <> '..')
{
$extension = strtoupper(substr($imgFile, strrpos($imgFile, '.')));
if (in_array($extension, $extensions))
{
$imageFiles[] = $imgFile;
}
}
}
closedir($openDir);
foreach ($imageFiles as $k => $v)
{
$withoutExt = preg_replace("/\\.[^.\\s]{3,4}$/", "", $v);
$hyphenlessName = str_replace("-"," ",$withoutExt);
$formattedName = strlen($hyphenlessName) > 24 ? substr($hyphenlessName,0,24)."..." : $hyphenlessName;
if ($category == 'featured'){
if (strpos($formattedName,'netent') !== false) {$finalName = trim($formattedName, 'netent');}
else if (strpos($formattedName,'mg') !== false) {$finalName = trim($formattedName, 'mg');}
else { $finalName = $formattedName;}
}
else { $finalName = $formattedName;}
echo '<div class="games-list-item"><div class="game-container">';
echo '<a href=""><img src="'.get_bloginfo('url').'/'.$directory.'/'.$v.'" alt=""/>';
echo '<h3>'.$finalName.'</h3></a>';
echo '<div class="game-hover">';
echo '<img src="'.get_bloginfo('url').'/'.$directory.'/'.$v.'" alt=""/>';
echo '<div class="game-buttons-holder"><a class="play_now_btns" href="">Play Now</a><a class="free_play_btns" href="">Free Play</a></div></div><!--.game-hover-->';
echo '</div><!--.game-container--></div><!--.games-list-item-->';
}
}
/////////////// MY APPROACH RESULTS ///////////////
When I load the Wordpress Page using the Page Template that calls this function, it loads the images flawlessly.
But then, I have to change the games category dynamically using jQuery, I take the category/folder name, save it in a JavaScript variable and send it to a php that grabs it and passes it to the fetch_game_images();
function.
My jQuery GET
looks like this:
$('#sidebar-list-menu li a').live('click', function(){
event.preventDefault();
var category = $(this).data('category');
//alert(cat);
//$('div.games-grid').html('<span class="loadingNotification">LOADING</span>');
$.get("<?php bloginfo('template_directory'); ?>/games-list-loader.php?category="+category,
function(data){
//alert(data);
//$('span.loadingNotification').remove();
$('div.games-grid').html(data);
});
});
And the PHP File (located in the same theme directory) looks like this:
<?php $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );
$cat = $_REQUEST['category'];
fetch_game_images($cat);
?>
<div class="clear"></div>
/////////////// THE ISSUE ///////////////
The issue is that when I click on the sidebar link that triggers the GET
call and tries to fetch the data from the PHP, it outputs the following error:
Warning: opendir(wp-content/games/featured/) [function.opendir]: failed to open dir: No such file or directory in [...]
Which shouldn't happen at all, since both my Page Template and my custom PHP file are in the same directory and load Wordpress core to use functions.php
.
/////////////// THE QUESTION ///////////////
Does anybody know what could be the reason behind this behaviour? I read most of the google results I could find about this opendir()
error, but most of them seem to point at a wrong path, but mine seem to be correct.
Others say that it might be a permissions issue. But I got my folders set to 755 and the files to 644.
So, any help would be great!
Thank you.
<======== /////////// EDIT ///////// =========>
Thanks to the help by @CBroe, I figured it out:
Instead of using a loose path like
"wp-content/games/".$category."/"
I changed it to a server-relative one
ABSPATH . "/wp-content/games/".$category."/"
And then I just changed the output to match the changes:
echo '<img src="'.get_bloginfo('url').'/wp-content/games/'.$category.'/'.$v.'" alt=""/>';
This works from both locations and solved the GET
issue.