WordPress

php — Jak sprawić, aby ten krótki kod nie odświeżał całej strony, gdy używam go na interfejsie użytkownika

  • 10 stycznia, 2024
  • 5 min read
php — Jak sprawić, aby ten krótki kod nie odświeżał całej strony, gdy używam go na interfejsie użytkownika


wszyscy, mam ten kod, który sprawia, że ​​wszystkie pliki w folderze są widoczne i możliwe do pobrania na interfejsie za pomocą krótkiego kodu. Ale chcę umieścić go w zaawansowanych zakładkach Essential Addons, ale nie do końca ze sobą współpracują, ponieważ za każdym razem, gdy szukam pliku, przechodzę do strony 2 lub czegoś innego, strona odświeża się i już mnie nie ma ta sama karta, co ja. Pomyślałem więc, że moim rozwiązaniem będzie uniemożliwienie odświeżania strony podczas interakcji z interfejsem kodu. Próbowałem używać AJAX, ale moje słabe umiejętności prawie zniszczyły moje połączenie ze stroną internetową (wszędzie 403). Pamiętaj, że jestem BARDZO amatorskim programistą i bieżący kod stworzyłem przy pomocy sztucznej inteligencji. Zwracam się do Was, mam nadzieję, że dobrze opisałem problem. Jeśli będziecie w stanie mi pomóc, będę dozgonnie wdzięczny. Kod poniżej:

function list_files_shortcode($atts) {
    // Set default attributes
    $atts = shortcode_atts(
        array(
            'folder' => '', // Replace with your default path
        ),
        $atts,
        'list_files'
    );

    // Get the absolute path of the folder on the server
    $absolute_folder_path = ABSPATH . $atts['folder'];

    // Check if the folder exists
    if (!is_dir($absolute_folder_path)) {
        return 'The folder does not exist.';
    }

    // Get the name of the current folder
    $current_folder_name = basename($absolute_folder_path);

    // Get the name of the parent folder
    $parent_folder_name = basename(dirname($absolute_folder_path));

    // Check if there is a search term
    $search_term = isset($_GET['search_term']) ? sanitize_text_field($_GET['search_term']) : '';

    // Get the list of files in the folder
    $files = scandir($absolute_folder_path);

    // Filter files based on the search term and exclude "." and ".."
    $filtered_files = array_filter($files, function ($file) use ($search_term) {
        return $file != "." && $file != ".." && (empty($search_term) || stripos($file, $search_term) !== false);
    });

    // Calculate the total number of files
    $total_files = count($filtered_files);

    // Set the number of files per page
    $files_per_page = 10;

    // Calculate the total number of pages
    $total_pages = ceil($total_files / $files_per_page);

    // Get the current page number from the 'page' parameter
    $current_page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;

    // Calculate the starting index of the array for the current page
    $start_index = ($current_page - 1) * $files_per_page;

    // Get the files for the current page
    $files_current_page = array_slice($filtered_files, $start_index, $files_per_page);

    // Start the HTML output
    $output="";

    // Add folder names above the search bar
    $output .= '<p class="folder-name" style="padding: 10px; border: 1px solid #ccc;">Current Folder Name: ' . $current_folder_name . '</p>';
    $output .= '<p class="folder-name" style="padding: 10px; border: 1px solid #ccc;">Parent Folder Name: ' . $parent_folder_name . '</p>';

    // Add search box, search button, and clear search button with styles
    $output .= '<form action="" method="get" style="margin-top: 20px; padding: 10px; border: 1px solid #ccc;">';
    $output .= '<div style="display: flex; align-items: center;">';
    $output .= '<input type="text" name="search_term" placeholder="Enter file name" value="' . esc_attr(isset($_GET['search_term']) ? $_GET['search_term'] : '') . '" style="flex: 1; padding: 8px; border: 1px solid #ccc;">';
    $output .= '<input type="hidden" name="folder" value="' . esc_attr($atts['folder']) . '">';
    $output .= '<input type="submit" value="Search" style="margin-left: 10px; padding: 8px; border: 1px solid #ccc;">';
    
    // Add the Clear Search button
    if (isset($_GET['search_term']) && !empty($_GET['search_term'])) {
        $output .= '<input type="button" value="Clear Search" onclick="location.href=\'' . esc_url(remove_query_arg('search_term')) . '\'">';
    }

    $output .= '</div>';
    $output .= '</form>';

    // Start the HTML output for the list of files
    $output .= '<ul class="files-list">';

    // Loop through the files of the current page
    foreach ($files_current_page as $file) {
        // Build the complete URL for the file
        $file_url = site_url($atts['folder'] . " . $file);

        // Add a list item for each matching file
        $output .= '<li><i class="fas fa-file"></i><a href="' . esc_url($file_url) . '">' . $file . '</a></li>';
    }

    // Close the list
    $output .= '</ul>';

    // Add page navigation
    if ($total_pages > 1) {
        $output .= '<div class="pagination">';
        for ($i = 1; $i <= $total_pages; $i++) {
            $output .= '<a href="' . esc_url(add_query_arg('page', $i)) . '" ' . ($i == $current_page ? 'class="current-page"' : '') . '>' . $i . '</a>';
        }
        $output .= '</div>';
    }

    // Add a message if no files are found
    if (empty($files_current_page) && !empty($search_term)) {
        $output .= '<p>No files found.</p>';
    }

    // Return the generated output
    return $output;
}

// Register the shortcode
add_shortcode('list_files', 'list_files_shortcode');

Dziękuję wam wszystkim

Warto przeczytać!  Jak naprawić przekierowanie WordPress do starej domeny po migracji


Źródło