Adding a New File or Template to a WordPress Theme Without FTP Access

March 8th, 2013 - Posted by Steve Marks to PHP, Web Development.

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:

<?php touch('wp-content/themes/your_theme_directory/your_new_file_name.php');?>

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.

Tags: , ,
This entry was posted on Friday, March 8th, 2013 at 9:10 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (1)
  1. Danny says:

    Thanks a lot, this was very helpful