Title: help needed edit file from plugin
Last modified: August 21, 2016

---

# help needed edit file from plugin

 *  Resolved [john_aku](https://wordpress.org/support/users/john_aku/)
 * (@john_aku)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/help-needed-edit-file-from-plugin/)
 * i am writing a plugin and i want it to have the possibility to change its css
   from a textarea in the plugin section.
 * I am able to code the metabox, the textarea(which loads the css file) but i cannot
   save it, when i press the button. The code is as follows. Please help me
 *     ```
       add_action('add_meta_boxes', 'edit_meta_box');
   
       function edit_meta_box() {
        add_meta_box("edit", "Editor", 'view_edit_box', "resp_edit", "normal");
   
       }
   
       function view_edit_box() {
   
       	$file = stripslashes('style.css');
   
       	$plugin_files = get_plugin_files($file);
       	$file = validate_file_to_edit($file, $plugin_files);
       	$real_file = WP_PLUGIN_DIR . '/' . $file;
   
       	if( isset($_POST['newcontent']) ) {
       		$newcontent = stripslashes($_POST['newcontent']);
       		if ( is_writeable($real_file) ) {
       				$f = fopen($real_file, 'w+');
       				file_put_contents($f, $newcontent);
       				fclose($f);
       		}
       		//echo $newcontent;
       	}
   
       	$content = file_get_contents($real_file );
       	$content = esc_textarea($content);
   
       	$html1 = '<form action="" method="post">
       				<table class="form-table"><tbody><tr valign="top"><td>
       				<textarea cols="85"
       						  rows="80"
       						  name="newcontent"
       						  id="newcontent1" tabindex="1">'.$content.'
       			 	</textarea>
       				</td></tr></tbody>
       			</table>
       			<input type="submit" name="sub" value="Update File">
       			</form>';
       		echo $html1;	
   
       }
       ```
   

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

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/help-needed-edit-file-from-plugin/#post-4718954)
 * You cannot handle a form POST with code inside a meta box callback. The metabox
   ends up inside the main post form `<form>` tags, so you are creating nested form
   tags is one problem. The other is the page that the POST actually goes to does
   not know how to handle your metabox data, and it is not necessarily calling your
   callback again on top of it.
 * You need to hook a different filter or action to handle your meta box POST data,
   one that fires when the main form is submitted, such as ‘save_post’.
 *  Thread Starter [john_aku](https://wordpress.org/support/users/john_aku/)
 * (@john_aku)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/help-needed-edit-file-from-plugin/#post-4718966)
 * Great
 * I added this hook and it all works perfect
 * add_action(‘save_post’, ‘view_edit_box’);
 * Thank you

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

The topic ‘help needed edit file from plugin’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 2 replies
 * 2 participants
 * Last reply from: [john_aku](https://wordpress.org/support/users/john_aku/)
 * Last activity: [12 years, 3 months ago](https://wordpress.org/support/topic/help-needed-edit-file-from-plugin/#post-4718966)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
