WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] help needed edit file from plugin (3 posts)

  1. john_aku
    Member
    Posted 4 months ago #

    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;	
    
    }
  2. bcworkz
    Member
    Posted 4 months ago #

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

  3. john_aku
    Member
    Posted 4 months ago #

    Great

    I added this hook and it all works perfect

    add_action('save_post', 'view_edit_box');

    Thank you

Reply

You must log in to post.

About this Topic

Tags

No tags yet.