【问题标题】:How can I embed external content in a WordPress widget?如何在 WordPress 小部件中嵌入外部内容?
【发布时间】:2018-10-22 13:46:51
【问题描述】:

我想在 WordPress 小部件中显示下载链接。要下载的文件位于站点根目录的 download 子文件夹中,因此可以通过 FTP 上传。文件名和要显示的下载链接文本应存储在同一文件夹中的简单文本文件中。

假设 WordPress 安装在 www.mysite.com 上。文件名为setup_1_0.zip,链接显示为Setup 1.0

我对如何存储这些信息的文件格式持开放态度,只要我也可以通过 FTP 上传该文件。

如何将此信息嵌入到自定义 HTML 小部件中以获取包含从该文件中获取的文本的有效下载链接?

【问题讨论】:

    标签: html wordpress embed


    【解决方案1】:

    如何在 WordPress 中自动化上传最新软件的构建和下载链接创建过程?

    根据您的逻辑。 您正在尝试自动执行最新软件版本的下载过程。

    您不想手动更新内容,只想将最新版本上传到/download/ 文件夹。 (仅使用 FTP 删除您的最新版本;仅此而已

    我会这样做:

    参考这些问题:

    Get the latest file addition in a directory

    How to force file download with PHP

    我提出了两个解决方案:第一个是两个单独的代码,第二个是内联代码。 仅用于教育目的


    第一个解决方案:快速而简短的使用

    (您可能需要一种方法或插件来激活在 Widget 中运行的 PHP;这个插件有帮助 PHP Code Widget

    <?php
    $path = "download/"; 
    $latest_ctime = 0;
    $latest_filename = '';    
    $d = dir($path);
    while (false !== ($entry = $d->read())) {
      $filepath = "{$path}/{$entry}";
      // could do also other checks than just checking whether the entry is a file
      if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
        $latest_ctime = filectime($filepath);
        $latest_filename = $entry;
      }
    }
    echo '<a href="http://www.example.com/'. $path .$latest_filename.'">Download '. $latest_filename . '</a>';
    ?>
    

    第二种解决方案:

    (同样,您可能需要一种方法或插件来激活在 Widget 中运行的 PHP;这个插件有助于 PHP Code Widget

    A)http://www.example.com/download.php中创建download.php

    添加以下代码:

    <?php
    $path = "download"; 
    $latest_ctime = 0;  //ctime stands for creation time.
    $latest_filename = '';    
    $d = dir($path);
    while (false !== ($entry = $d->read())) {
      $filepath = "{$path}/{$entry}";
      // could do also other checks than just checking whether the entry is a file
      if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
        $latest_ctime = filectime($filepath);
        $latest_filename = $entry;
      }
    }
    // echo $latest_filename; un-comment to debug
    $file_url = 'http://www.example.com/download/'.$latest_filename;
    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
    readfile($file_url); // do the double-download-dance (dirty but worky)
    ?>
    

    B) 在您的 WordPress HTML Widget 中添加以下代码

    <?php
    $path = "download"; 
    $latest_ctime = 0;
    $latest_filename = '';    
    $d = dir($path);
    while (false !== ($entry = $d->read())) {
      $filepath = "{$path}/{$entry}";
      // could do also other checks than just checking whether the entry is a file
      if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
        $latest_ctime = filectime($filepath);
        $latest_filename = $entry;
      }
    }
    echo '<a href="http://www.example.com/download.php">Download '. $latest_filename . '</a>';
    ?>
    

    进一步解释:

    A) 负责自动下载最新的软件版本。

    B) 负责显示最新版本名称和创建链接。

    现在,您只需将一个文件上传到您的最新版本的 /download/ 文件夹(setup_1_0.zipsetup_1_1.zipsetup_1_2.zip ...等。建议的解决方案将检查创建日期,而不管文件的名字。)

    重要提示:可以看到最新的文件检查器功能重复了两次;一次在download.php 和一次在 WordPress 小部件中。因为如果我们合并在一个文件中,我们会得到 header 已经发送的错误。

    请回答您的问题?欢迎反馈。

    【讨论】:

    • 像魅力一样工作。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    相关资源
    最近更新 更多