【问题标题】:How do I create a PHP file in WordPress using dashboard如何使用仪表板在 WordPress 中创建 PHP 文件
【发布时间】:2021-03-19 11:04:20
【问题描述】:

我想在不使用 ftp/c-panel 的情况下通过 WordPress 仪表板创建一个 PHP 文件,正如我检查过的那样,我发现您可以通过在 header.php 中添加代码来创建一个 PHP 文件,但我没有标题。 php 在子主题中也是如此,并且无法访问 cPanel。有什么建议我可以通过在functions.php中添加一些代码来从WordPress仪表板外观->编辑器创建php文件吗?

提前谢谢你。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

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

    【讨论】:

    • 是的,这段代码可以正常工作,但是你想在你的主题文件夹中授予创建新文件的权限。
    • @kuldipMakadiya 您有权创建文件首先需要您的主题文件夹中的权限
    【解决方案2】:

    我已经修改了答案,以便能够:

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

    【讨论】:

      猜你喜欢
      • 2011-06-02
      • 2014-10-01
      • 2021-11-30
      • 2012-01-29
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      • 2021-12-13
      • 2020-04-16
      相关资源
      最近更新 更多