I’ am trying to get a list of file names from a directory folder and append them to an existing select dropdown filed as options.
add_action ('wp_head', 'testf');
function testf() {
$customer = md5( WC()->session->get_customer_id());
echo "<script>console.log('Debug Objects: " . $customer . "' );</script>";
$dir = '/wp-content/uploads/wapf/655dd0b1b2276/'.$customer.'';
echo "<script>console.log('CFilePath: " . $dir . "' );</script>";
$files = glob($dir . "*.png,jpg,jpeg,tif ");
foreach($files as $file)
{
echo "<script language= 'JavaScript'>alert(' . $file . ');</script>";}
$('select[class="user-file-uploads[]"]').append("<option value='" +$file+ "'>" +$file+ "</option>");
}
What is working is both $customer and $dir – console log screenshot below.
We added an alert for testing purposes as in this case their is only one file that matches the file extension types declared in the glob – the file being a .png.
No alert is showing, and similarly the select dropdown field does not display the expected option. As you can see we are trying to select the select input field by class name.
What am I doing wrong?
All help appreciated
2
Answers
A couple of problem are:
Your path is not correct you need to separate it with a slash:
You also need to update your pattern like this in order to match any of the extensions:
From the
manual
:I hope this updated code helps that fixes your code which has the
glob
pattern without the braces {}, also the javaScript code should be wrapped in ajQuery(document).ready
block to ensure it runs after the DOM is fully loaded.