After starting my own Web Development Agency, I have faced many different scenarios. I have seen clients which only give WordPress Dashboard access and expect heavy modification in the theme or WordPress features. In a recent assignment we had to create an exclusive iPhone look alike e-bookshelf but without having FTP access. We tend to avoid creating new PHP file, but at one point it could not be avoided. So here I am going to share my experience on how to create new theme file without ftp using WordPress admin.
In theory, we are going to use Unix command touch to create a new file by executing a command from an already existing PHP file. You can create any type of file and as many as you want to. Now let us see how you can create a new theme file from wordpress admin by following simple steps –
1) Know your current theme name and directory.
You can know your current theme name and directory from the Appearance > Editor option. Just click on the Editor and it will show you the list of your already existing theme files and the name of the theme. All the themes are located inside wp-content/themes/ directory, so all you need is to add your theme name after the themes/. In our example the path would be – wp-content/themes/digimantra
2) Create the necessary PHP code to create the new file.
Now that you have the path to your theme directory, let’s create the PHP code which will magically create a new theme file in your current WordPress theme directory. The code is –
<?php touch(‘wp-content/themes/digimantra/new-template.php’); ?>
In the above line of code we are using the unix command touch which will create the new file new-template.php inside the current wordpress theme directory which is digimantra in our case.
3) Place the code, and remove after successful creation of the file.
Next, we are going to place this line of code into any existing PHP file using the built-in theme editor as discussed above. I recommend placing this code inside header.php since it is going to execute on every page of the website. Now open your website’s homepage. As soon as you will open the website, this line of code will be executed from header.php and a new file new-template.php will be created.
Finally remove or comment this line of code from header.php since it is not needed anymore, as long as you are not planning to create more files.
Notable facts–
- You can create multiple files using the multiple lines of the above PHP code.
- touch unix command modifies the access & modification time of a file to the current time (if you not specified), hence it is same even if your website is live and header.php gets executed multiple times. It will never ooze out an error in the frontend.
- Note – You should place the code outside the PHP loop, if you are going to paste the code from above. Apparently, it has the php start and end tag. If you paste this code inside already opened PHP tags, it will give a fatal error and the whole theme can crash until you fix the file using FTP.
- You can edit the newly create files using the theme editor.
Hope this trick will help you.
Stay Digified!!
Sachin Khosla