• 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)
  • 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

    (@imehedidip)

    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

    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() for more info.

    Thread Starter Mehedi

    (@imehedidip)

    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?

    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

    (@imehedidip)

    What do you suggest me to use? please help I’ve not got much idea at this section just yet.

    Thread Starter Mehedi

    (@imehedidip)

    just to let you know, i am currently testing it in my localhost

    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

    (@imehedidip)

    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)?

    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

    (@imehedidip)

    Ok. Thanks!

    Dion

    (@diondesigns)

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.