WordPress - a list of PDF files in PHP format

I wrote 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 added it to my functions file, and I can successfully apply it with the page template and display 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 on them, it causes a 404 error, and I cannot understand why. I tried to reset my permalinks and check the FTP path (its 100% correct).

If I try to move my / files / folder to the server root using

$directory = home_url()."/htdocs/files".$sub_folder;

He returns

Warning: scandir ( http: // localhost / wordpress / files / application_notes ): could not open directory: not implemented in C: \ XAMPP \ application \ WordPress \ HTDOCS \ content-content \ Themes \ twentythirteen-child \ user_functions.php in line 5

Files are mainly PDF files ("how_to_do_stuff.pdf", etc.).

, ?

EDIT1: , , WordPress 404 :

var_dump ($ )

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"
}

, :

var_dump ($ )

string(57) "http://localhost/wordpress/htdocs/files/application_notes"

var_dump ($ scanned_directory)

NULL
+4
1

... URL-, , :

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>";
    }
}

:

get_stylesheet_directory_uri() . 
// http://example.com/wp-content/themes/yourtheme

'/files/' . $sub_folder . '/' . $value
// /files/subfolder/filename.txt
+3

Source: https://habr.com/ru/post/1598878/


All Articles