A quick 3 step post here on how to create or add a new PHP file in your WordPress theme directory without having FTP access.
If you don’t even know what FTP access is, this post is for you :)
And it doesn’t have to be a PHP file, you can create any file with this method.
Here are a couple assumptions before moving forward:
- You have a self-hosted WordPress website (wordpress.org version)
- You have ADMIN access to your WordPress dashboard control panel area
- Assuming you can use the Theme Editor (Appearance -> Editor) and save the changes to files
To create a new file in your WordPress theme directory…
Step 1: Know Your Theme Directory & File Name
You will need to know the directory in which your current active theme is running (and thus, the directory to which you want to add a new file).
Hop over to Appearance->Themes … You see the current theme that’s running (it’s the bigger one relative to any that are under it).
You will have to activate a different theme than the one which your site is running on, then look for “All of this theme’s files are located in /themes/YOUR_THEME_DIR.” — Once you learn the theme’s directory, quickly reactivate the theme your site was running on :)
Also, know the name of the file you wish to create as that will make this much easier. Know the extension too (is it a .html, .php, etc… type of file?)… I call this FILE_NAME.
Step 2: The (simple) PHP code
Using touch(); PHP function, which creates a new file in the specified directory. This is how I suggest doing it (and admittedly, there are cleaner ways, but this will get the job done, quickly and effectively)…
Open header.php — then write this code in the very top of the file:
Replace YOUR_THEME_DIR with the directory in which your WordPress theme lives.
Then, replace FILE_NAME with the name of the file you want to create.
Once that’s all done, save Header.php and go to the homepage of your site.
As soon as the homepage loads, it will create that new template or theme file.
Step 3: Remove the PHP Code
Quickly go back to your theme editor and remove that line of PHP code I told you to put in there earlier.
Don’t believe me? – Go to your theme editor (Appearance -> Editor) and refresh that page. You should see your new FILE_NAME on the right side.
I hope this how to helped you add or create a new PHP or HTML theme/template file in your WordPress theme directory.
If you had troubles, I recommend hiring a professional WordPress website developer :)