I've written this code:
function userfunc_get_file_links($sub_folder) {
if ( is_user_logged_in() ) {
$directory = get_stylesheet_directory()."/files/".$sub_folder;
$scanned_directory = array_diff(scandir($directory), array('..', '.'));
echo "<ul class='support_links'>";
foreach ($scanned_directory as $key => $value) {
echo "<li><a href='files/".$value."'>".$value."</a></li>";
}
echo "</ul>";
}
}
I've added it to my functions file, and I can successfully apply it with a page template and it displays a list of file links.
(Template file):
<?php
/*
Template Name: supportpage
*/
?>
<?php get_header(); the_post(); ?>
<div id="content" class="inner-page">
<div class="main-content">
<div class="brack-cum">
<div class="inner-small">
<?php
if (function_exists('wp_bac_breadcrumb')) {wp_bac_breadcrumb();}
?>
</div>
</div>
<div class="title">
<div class="inner-small">
<h2><?php the_title(); ?></h2>
</div>
</div>
<div class="content-page inner-small">
<?php
the_content();
?>
</div>
</div>
<?php get_footer(); }?>
However, when I click them it brings up a 404 error and I can't work out why. I've tried resetting my permalinks and checking the FTP path (its 100% correct).
If I try moving my /files/ folder to the root directory of the server with
$directory = home_url()."/htdocs/files".$sub_folder;
It returns
Warning: scandir(http://localhost/wordpress/files/application_notes): failed to open dir: not implemented in C:\xampp\apps\wordpress\htdocs\wp-content\themes\twentythirteen-child\user_functions.php on line 5
The files are pdf files for the most part ("how_to_do_stuff.pdf" etc).
Any ideas on the steps I could take from here?
EDIT1: With the original code displaying the links but returning a WordPress 404 on click:
var_dump($directory)
string(93) "C:\xampp\apps\wordpress\htdocs/wp-content/themes/twentythirteen-child/files/application_notes"
var_dump($scanned_directory)
array(3) {
[2]=>
string(9) "test1.pdf"
[3]=>
string(9) "test2.pdf"
[4]=>
string(9) "test3.pdf"
}
With the code that produces the warning:
var_dump($directory)
string(57) "http://localhost/wordpress/htdocs/files/application_notes"
var_dump($scanned_directory)
NULL
You're close...but you need to output URLs if you want people to be able to click on the links:
function userfunc_get_file_links($sub_folder) {
if ( is_user_logged_in() ) {
$directory = get_stylesheet_directory()."/files/".$sub_folder;
$scanned_directory = array_diff(scandir($directory), array('..', '.'));
echo "<ul class='support_links'>";
foreach ($scanned_directory as $key => $value) {
echo "<li><a href='".get_stylesheet_directory_uri() . "/files/" . $sub_folder . "/" . $value . "'>".$value."</a></li>";
}
echo "</ul>";
}
}
Here's what each part should do:
get_stylesheet_directory_uri() .
// http://example.com/wp-content/themes/yourtheme
'/files/' . $sub_folder . '/' . $value
// /files/subfolder/filename.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With