Adding a New File or Template to a WordPress Theme Without FTP Access
The WordPress platform contains a feature allowing direct editing of theme and plugin files through the backend when logged in. Before today I had never used this functionality due to always having had access to the actual files, for example, via FTP.
Today was different however and I found myself working on a website where I had no access to the filesystem. Making changes would have to be done by logging into the backend of WordPress and editing the files contents there. This was all pretty self-explanatory and, aside from being slower and the editor missing some useful features, I managed to make the majority of the changes (note the word ‘majority’).
One of the things that I couldn’t do when editing the theme was to create a new file or template as, to my surprise, the ability to add files just doesn’t exist. There was no way (and no time) to gain direct access to the filesystem so I had to think outside of the box.
What I came up with as a way to add a new file with no FTP access was as follows:
1. Log into WordPress and navigate to ‘Appearance > Editor’.
2. On the right-hand side you will see a list of files in the current theme.
3. Click the file in your theme named header.php in order to open its contents.
4. Add the following code as the first line of the file before everything else:
Replace ‘your_theme_directory‘ for, well, the name of your theme directory (duh!), and ‘your_new_file_name.php‘ for.. yup.. you’ve got it… the name of the file that you wish to create.
Note: touch() is a PHP function used to sets access and modification times on files. If the file doesn’t exist however it does create it which is what we want.
5. Visit the homepage of your website and the code you entered above will be executed.
6. Navigate back to ‘Appearance > Editor’ in the WordPress backend and remove the code you added in step 4 to prevent it from being executed again.
7. You should also notice that the new file you created above is present in the right-hand side list of files ready for you to edit to your heart’s content.
So there we have it. A new file created in your theme with just WordPress login details. The steps above can also be used to create new files when developing a plugin. Simply change the filepath when executing the touch() command to match that of the plugin.