【发布时间】:2021-03-19 11:04:20
【问题描述】:
我想在不使用 ftp/c-panel 的情况下通过 WordPress 仪表板创建一个 PHP 文件,正如我检查过的那样,我发现您可以通过在 header.php 中添加代码来创建一个 PHP 文件,但我没有标题。 php 在子主题中也是如此,并且无法访问 cPanel。有什么建议我可以通过在functions.php中添加一些代码来从WordPress仪表板外观->编辑器创建php文件吗?
提前谢谢你。
【问题讨论】:
我想在不使用 ftp/c-panel 的情况下通过 WordPress 仪表板创建一个 PHP 文件,正如我检查过的那样,我发现您可以通过在 header.php 中添加代码来创建一个 PHP 文件,但我没有标题。 php 在子主题中也是如此,并且无法访问 cPanel。有什么建议我可以通过在functions.php中添加一些代码来从WordPress仪表板外观->编辑器创建php文件吗?
提前谢谢你。
【问题讨论】:
将此代码放入您的 function.php 文件并运行该站点
add_action( 'init', 'createnewfile' );
function createnewfile()
{
$myfile = fopen(dirname(__FILE__)."/newfile.php", "w") or die("Unable to open file!");
$txt = "test\n";
fwrite($myfile, $txt);
$txt = "test\n";
fwrite($myfile, $txt);
fclose($myfile);
}
【讨论】:
我已经修改了答案,以便能够:
function createnewfile() {
$filename = dirname(__FILE__)."/newfile.php";
if (file_exists($filename)){
//file exists, then it does nothing.
} else{
$myfile = fopen(dirname(__FILE__)."/newfile.php", "w") or die("Unable to open file!");
$txt = "Tienes que editar este archivo2\n";
fwrite($myfile, $txt);
fclose($myfile);
}
}
add_action( 'after_switch_theme', 'createnewfile' );
【讨论】: