use WordPress theme / plugin editor in my plugin

Is there a way to use the wordpress theme / plugin editor inside my plugin to give the user the ability to edit the css file of my plugin ?

Im building a plugin that gives the user an option to add some CSS. First i was going to use just a textarea but then i thought it would be pretty could if i can use the WordPress editor that will open my style.css file.
I could put just a link to http://www.xxxxxx.com/wp-admin/plugin-editor.php but it would be great if i can integrate it into my plugin.

Regards

2 Answers
2

To start with you could use the following code. For security you could also add some nonce check and even use the settings api. Here plugin-test is your plugin folder name.

$file = stripslashes('plugin-test/style.css');
$plugin_files = get_plugin_files($file);
$file = validate_file_to_edit($file, $plugin_files);
$real_file = WP_PLUGIN_DIR . "https://wordpress.stackexchange.com/" . $file;

if( isset($_POST['plugin_test_settings']['newcontent']) ) {
    $newcontent = stripslashes($_POST['plugin_test_settings']['newcontent']);
    if ( is_writeable($real_file) ) {
            $f = fopen($real_file, 'w+');
            fwrite($f, $newcontent);
            fclose($f);
    }
}

$content = file_get_contents( $real_file );

$content = esc_textarea( $content ); ?>
<table class="form-table">
    <tbody>
        <tr valign="top">
            <td>
                <textarea cols="70" rows="25" name="plugin_test_settings[newcontent]" id="newcontent" tabindex="1"><?php echo $content ?></textarea>
            </td>
        </tr>
    </tbody>
</table>

Leave a Comment