如何使用dashboard在WordPress中创建PHP文件
我想通过WordPress dashboard创建一个PHP文件,而不使用ftp/c-panel,正如我所检查的,我发现可以通过在header.PHP中添加代码来创建一个PHP文件,但我在子主题中也没有header.PHP,并且无法访问cPanel。有没有建议我如何通过在functions.php中添加一些代码,从WordPress dashboard appearance->editor创建php文件如何使用dashboard在WordPress中创建PHP文件,php,wordpress,Php,Wordpress,我想通过WordPress dashboard创建一个PHP文件,而不使用ftp/c-panel,正如我所检查的,我发现可以通过在header.PHP中添加代码来创建一个PHP文件,但我在子主题中也没有header.PHP,并且无法访问cPanel。有没有建议我如何通过在functions.php中添加一些代码,从WordPress dashboard appearance->editor创建php文件 提前感谢您。将此代码放在function.php文件中,然后运行该网站 add_action
提前感谢您。将此代码放在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您有权首先在主题文件夹中创建文件,但需要该权限
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' );