How do I create a PHP file in WordPress using dashboard

I want to create a PHP file through WordPress dashboard without using ftp/c-panel, as I have checked and I found that you can create a PHP file through adding code in header.php But I don’t have header.php in child theme as well and no access to cPanel. Any suggestion how I can create php file from WordPress dashboard appearance->editor by putting some code in functions.php?

Thank you in advance.

Related posts

Leave a Reply

2 comments

  1. Put this code in your function.php file and run the site

    add_action( 'init', 'createnewfile' );
    function createnewfile()
    {
        $myfile = fopen(dirname(__FILE__)."/newfile.php", "w") or die("Unable to open file!");
        $txt = "testn";
        fwrite($myfile, $txt);
        $txt = "testn";
        fwrite($myfile, $txt);
        fclose($myfile);
    
    }
    
  2. I have modified the answer to be able to:

    • The function only runs when the theme is “activated”.
    • And, if for some reason the file exists, don’t overwrite the existing one.
    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 archivo2n";
            fwrite($myfile, $txt);
            fclose($myfile);
        }
    }
    add_action( 'after_switch_theme', 'createnewfile' );