如何使用仪表板在WordPress中创建PHP文件

时间:2016-06-14 13:25:33

标签: php wordpress

我想通过WordPress仪表板创建一个PHP文件而不使用ftp / c-panel,因为我已经检查过,我发现你可以通过在header.php中添加代码来创建一个PHP文件但是我没有头文件。 php在子主题中也没有访问cPanel。有什么建议我如何通过在functions.php中添加一些代码从WordPress仪表板外观 - >编辑器创建php文件?

提前谢谢。

2 个答案:

答案 0 :(得分:2)

将此代码放入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);

}

答案 1 :(得分:0)

我修改了答案以便能够:

  • 该功能仅在主题“激活”时运行。
  • 而且,如果由于某种原因该文件存在,请不要覆盖现有文件。
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' );
相关问题