Support » Plugins » Hacks » help needed edit file from plugin

  • Resolved john_aku


    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);
    		//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"
    						  id="newcontent1" tabindex="1">'.$content.'
    			<input type="submit" name="sub" value="Update File">
    		echo $html1;	
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz


    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’.


    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.