如何在 WordPress 小部件中嵌入外部内容?
How can I embed external content in a WordPress widget?
我想在 WordPress 小部件中显示下载 link。要下载的文件位于站点根目录的 download 子文件夹中,因此可以通过 FTP 上传。下载文件的名称和要显示的文本 link 应存储在同一文件夹中的简单文本文件中。
假设 WordPress 安装在 www.mysite.com
上。文件名为setup_1_0.zip
,link显示为Setup 1.0
.
我对如何存储这些信息的文件格式持开放态度,只要我也可以通过 FTP 上传该文件。
如何将此信息嵌入到自定义 HTML 小部件中,以便使用从该文件中获取的文本进行有效下载 link?
如何在 WordPress 中自动上传最新软件的构建和下载 link 创建过程?
根据你的逻辑。
您正在尝试自动执行最新软件版本的下载过程。
您不想手动更新内容,只想将最新版本上传到 /download/
文件夹中。 (仅使用 FTP 删除最新版本;仅此而已)
我会这样做:
参考那些问题:
Get the latest file addition in a directory
How to force file download with PHP
我提出两个解决方案:第一个是两个separte代码,第二个是inline代码。
仅用于教育目的
第一个解决方案:快速而简短的用法:
(您可能需要一种方法或插件来激活 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) 负责显示最新构建名称并创建 link.
现在,您只需将一个文件上传到您的 /download/
文件夹,这是您的最新版本(setup_1_0.zip
、setup_1_1.zip
、setup_1_2.zip
...等。建议的解决方案将检查创建日期,而不管文件的名称。)
重要提示:可以看到最新的文件检查器功能重复了两次;一次在 download.php
中,一次在 WordPress Widget 中。因为如果我们合并在一个文件中,我们将得到 header already sent error.
请问这是否回答了您的问题?欢迎反馈。
我想在 WordPress 小部件中显示下载 link。要下载的文件位于站点根目录的 download 子文件夹中,因此可以通过 FTP 上传。下载文件的名称和要显示的文本 link 应存储在同一文件夹中的简单文本文件中。
假设 WordPress 安装在 www.mysite.com
上。文件名为setup_1_0.zip
,link显示为Setup 1.0
.
我对如何存储这些信息的文件格式持开放态度,只要我也可以通过 FTP 上传该文件。
如何将此信息嵌入到自定义 HTML 小部件中,以便使用从该文件中获取的文本进行有效下载 link?
如何在 WordPress 中自动上传最新软件的构建和下载 link 创建过程?
根据你的逻辑。 您正在尝试自动执行最新软件版本的下载过程。
您不想手动更新内容,只想将最新版本上传到 /download/
文件夹中。 (仅使用 FTP 删除最新版本;仅此而已)
我会这样做:
参考那些问题:
Get the latest file addition in a directory
How to force file download with PHP
我提出两个解决方案:第一个是两个separte代码,第二个是inline代码。 仅用于教育目的
第一个解决方案:快速而简短的用法:
(您可能需要一种方法或插件来激活 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
添加以下代码:
<?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) 负责显示最新构建名称并创建 link.
现在,您只需将一个文件上传到您的 /download/
文件夹,这是您的最新版本(setup_1_0.zip
、setup_1_1.zip
、setup_1_2.zip
...等。建议的解决方案将检查创建日期,而不管文件的名称。)
重要提示:可以看到最新的文件检查器功能重复了两次;一次在 download.php
中,一次在 WordPress Widget 中。因为如果我们合并在一个文件中,我们将得到 header already sent error.
请问这是否回答了您的问题?欢迎反馈。