1
votes

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.

2

2 Answers

3
votes

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 = "test\n";
    fwrite($myfile, $txt);
    $txt = "test\n";
    fwrite($myfile, $txt);
    fclose($myfile);

}
0
votes

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 archivo2\n";
        fwrite($myfile, $txt);
        fclose($myfile);
    }
}
add_action( 'after_switch_theme', 'createnewfile' );