user1295167
user1295167

Reputation: 101

how to write in to file in wordpress

I'm writting a plugin and there is an option to write css from admin side. Here I need to write css in to a specific file named style.css. For these i have used the php function <?php file_put_contents($file, $data, FILE_TEXT )?>. But it shows error like

file_put_contents(http://localhost/car/wp-content/plugins/networks-inv/tab/style.css) [function.file-put-contents]: failed to open stream: HTTP wrapper does not support writeable connections in C:\wamp\www\car\wp-content\plugins\networks-inv\admin\templates.php on line 49. I cant understand why these errors shows?. Is there any method to write a file in wordpress plugin

Upvotes: 4

Views: 11480

Answers (4)

FossMentor
FossMentor

Reputation: 21

You can dynamically fetch the plugin directory path by using the following WP function and then pass it into the file_put_contents function.

$upload_dir = plugin_dir_path( __DIR__ ).'/networks-inv/tab/style.css';
 
file_put_contents($upload_dir, $data, FILE_TEXT );

Upvotes: 1

Darryl Bassett
Darryl Bassett

Reputation: 1

I initially received the same warning, but determined cause to be setting file path as:

$file = ".\wp-content\plugins\logging.txt";

instead of

$file = ".\\\\wp-content\\\\plugins\\\\logging.txt";

File path should have two slashes instead of one.

I hope this resolves the problem of the incorrect path name.

Upvotes: 0

Justin Erswell
Justin Erswell

Reputation: 708

It may be because you are trying to write using a http method try using local file paths instead

Upvotes: 0

Rosmarine Popcorn
Rosmarine Popcorn

Reputation: 10967

You should write the System Path of the file ,not the URL.

You should :

$file = "car/wp-content/plugins/networks-inv/tab/style.css";
file_put_contents($file, $data, FILE_TEXT );

Upvotes: 0

Related Questions