Title: Create a PHP file on theme directory using a WordPress Plugin
Last modified: August 21, 2016

---

# Create a PHP file on theme directory using a WordPress Plugin

 *  [Mehedi](https://wordpress.org/support/users/imehedidip/)
 * (@imehedidip)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/)
 * Hi
 * I am creating a plugin to create WordPress Template Files for a theme. I am not
   sure how to create a PHP file using the plugin. I mean, is it possible to create
   a PHP file using a code in the plugin?
 * If so, can you kindly provide the code for it?
 * Thanks in advance!

Viewing 12 replies - 1 through 12 (of 12 total)

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144822)
 * You can. It’s not that hard.
 *     ```
       $file_location = '/path/to/file.php';
   
       $contents = '<?php echo "Hello World!";
   
       file_put_contents ($file_location, $contents);'
       ```
   
 *  Thread Starter [Mehedi](https://wordpress.org/support/users/imehedidip/)
 * (@imehedidip)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144826)
 * Hi,
 * Thanks for the code but it doesn’t works. I modified it a little bit, here is
   how it looked like:
 *     ```
       $file_location = get_bloginfo(url) . '/wp-content/themes/' . get_template();
   
       $contents = include ('single-products.php');
   
       file_put_contents ($file_location, $contents);
       ```
   
 * If I activate the theme, it shows a blank page.
 * please help
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144828)
 * `get_bloginfo(url)`
 * That’s what’s wrong. The blog URL is something like `http://www.mysite.com` and
   that’s not part of the servers file system. It needs to be the location on the
   file system in order to save it. You can’t save directly to a website address.
 * As it’s a plugin I’d suggest using something like this:
 * `$file_location = plugins_url( 'path/to-my.file.php' );`
 * Check out the docs for [plugins_url()](http://codex.wordpress.org/Function_Reference/plugins_url)
   for more info.
 *  Thread Starter [Mehedi](https://wordpress.org/support/users/imehedidip/)
 * (@imehedidip)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144829)
 * Hi
 * I think you didn’t understand my question? I want to create a new PHP file on
   my theme’s root directory (/wp-content/themes/theme-name/). Is it possible?
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144830)
 * Yes it is. But you need to know what the directory name is, not what the URL 
   is. There’s a very big difference there. Normally if you’re running that from
   a file in a plugin it would be something like `realpath ('../../../')` but you
   will need to check that for yourself to see if it right or not because things
   cna change between servers.
 *  Thread Starter [Mehedi](https://wordpress.org/support/users/imehedidip/)
 * (@imehedidip)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144831)
 * What do you suggest me to use? please help I’ve not got much idea at this section
   just yet.
 *  Thread Starter [Mehedi](https://wordpress.org/support/users/imehedidip/)
 * (@imehedidip)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144832)
 * just to let you know, i am currently testing it in my localhost
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144833)
 * What do you need to know? I’ve told you how to do it, how to get the right location,
   so that’s pretty much everything that you’ve aksed. What else do you need?
 *  Thread Starter [Mehedi](https://wordpress.org/support/users/imehedidip/)
 * (@imehedidip)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144834)
 * I am not sure what to replace with the “path” on `$file_location = plugins_url('
   path/to-my.file.php' );`
 * Just curious, is it possible to do it using Must Use Plugins (mu-plugins)?
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144835)
 * As I said in my last reply…
 * If you want it in the websites root folder you should use `realpath ('../../../')`.
 * But be aware that might not be the right path, so test it yourself.
 *  Thread Starter [Mehedi](https://wordpress.org/support/users/imehedidip/)
 * (@imehedidip)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144836)
 * Ok. Thanks!
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144937)
 * [**This should help.**](http://codex.wordpress.org/Function_Reference/get_template_directory)

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Create a PHP file on theme directory using a WordPress Plugin’ is closed
to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 12 replies
 * 3 participants
 * Last reply from: [Dion](https://wordpress.org/support/users/diondesigns/)
 * Last activity: [11 years, 10 months ago](https://wordpress.org/support/topic/create-a-php-file-on-theme-directory-using-a-wordpress-plugin/#post-5144937)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
