在我的插件中使用WordPress主题/插件编辑器

时间:2011-12-05 作者:chifliiiii

有没有办法在我的插件中使用wordpress主题/插件编辑器,让用户能够编辑我插件的css文件?

我正在构建一个插件,为用户提供添加一些CSS的选项。首先我打算只使用一个文本区域,但后来我想如果我可以使用Wordpress编辑器来打开我的样式,那就太好了。css文件。我可以放一个链接到http://www.xxxxxx.com/wp-admin/plugin-editor.php 但如果我能将其集成到我的插件中,那就太好了。

当做

2 个回复
最合适的回答,由SO网友:Joshua Abenazer 整理而成

首先,可以使用以下代码。为了安全起见,您还可以添加一些nonce检查,甚至可以使用settings api。在这里plugin-test 是插件文件夹名称。

$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 . \'/\' . $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>

SO网友:Ján Bočínec

如果您正在寻找更复杂的解决方案,我根据论文主题的代码编写了WP自定义文件编辑器插件http://diythemes.com/

<?php
/*
Plugin Name: WP Custom File Editor
Plugin URI: http://www.webikon.eu/
Description: Simple file editor
Version: 0.1
Author: Ján Bočínec
Author URI: http://johnnypea.wp.sk/
License: GPLv2 or later
*/

/* This code is based on code from Thesis theme http://diythemes.com/ */

// You can define your custom folder where are located files you would like to be editable. Default is current folder.
if (!defined(\'WPCFE_CUSTOM_FOLDER\'))
    define(\'WPCFE_CUSTOM_FOLDER\', __DIR__);

/**
 * You can define your own array of file paths.
 * If this is set, only these files are loaded into the editor
 */
// global $custom_files_array;
// $custom_files_array = array(\'custom.css\', \'hello.php\');

// register the admin menu
add_action(\'admin_menu\', \'wpcfe_add_menu\');
// save the changes
add_action(\'admin_post_wpcfe_file_editor\', array(\'wpcfe_custom_editor\', \'save_file\'));

// Add admin menu
function wpcfe_add_menu() {
    add_options_page(__(\'WP Custom File Editor\', \'wpcfe\'), __(\'WP Custom File Editor\', \'wpcfe\'), \'edit_themes\', \'wpcfe-file-editor\', array(\'wpcfe_custom_editor\', \'options_page\'));
}

/**
 * Outputs the Custom File Editor
 */
class wpcfe_custom_editor {

    function wpcfe_custom_editor($custom_files_array=\'\') {
        $this->custom_files = $custom_files_array;
    }

    function get_custom_files() {
        $files = array();
        if ( $this->custom_files )
            return $files = $this->custom_files;
        if ( !is_dir (WPCFE_CUSTOM_FOLDER) )
            return $files;
        $directory = opendir(WPCFE_CUSTOM_FOLDER); // Open the directory
        $exts = array(\'.php\', \'.css\', \'.js\', \'.txt\', \'.inc\', \'.htaccess\', \'.html\', \'.htm\'); // What type of files do we want?

        while ($file = readdir($directory)) { // Read the files
            if ($file != \'.\' && $file != \'..\' && (strpos($file, \'layout\') === false)) { // Only list files within the _current_ directory
                $extension = substr($file, strrpos($file, \'.\')); // Get the extension of the file

                if ($extension && in_array($extension, $exts)) // Verify extension of the file; we can\'t edit images!
                    $files[] = $file; // Add the file to the array
            }
        }

        closedir($directory); // Close the directory
        return $files; // Return the array of editable files
    }

    function is_custom_writable($file, $files) {
        if (!in_array($file, $files) && is_dir (WPCFE_CUSTOM_FOLDER))
            $error = "<p><strong>" . __(\'Attention!\', \'wpcfe\') . \'</strong> \' . __(\'For security reasons, the file you are attempting to edit cannot be modified via this screen.\', \'wpcfe\') . \'</p>\';
        elseif (!file_exists(WPCFE_CUSTOM_FOLDER)) // The custom/ directory does not exist
            $error = "<p><strong>" . __(\'Attention!\', \'wpcfe\') . \'</strong> \' . __(\'Your <code>\'.WPCFE_CUSTOM_FOLDER.\'/</code> directory does not appear to exist.\', \'wpcfe\') . \'</p>\';
        elseif (!is_file(WPCFE_CUSTOM_FOLDER . \'/\' . $file)) // The selected file does not exist
            $error = "<p><strong>" . __(\'Attention!\', \'wpcfe\') . \'</strong> \' . __(\'The file you are attempting does not appear to exist.\', \'wpcfe\') . \'</p>\';
        elseif (!is_writable(WPCFE_CUSTOM_FOLDER . \'/\' . $file)) // The selected file is not writable
            $error = "<p><strong>" . __(\'Attention!\', \'wpcfe\') . \'</strong> \' . sprintf(__(\'Your <code>/\'.WPCFE_CUSTOM_FOLDER.\'/%s</code> file is not writable by the server, and in order to modify the file via the admin panel, WP File Editor needs to be able to write to this file. All you have to do is set this file&#8217;s permissions to 666, and you&#8217;ll be good to go.\', \'wpcfe\'), $file) . \'</p>\';

        if ($error) { // Return the error + markup, if required
            $error = "<div class=\\"warning\\">\\n\\t$error\\n</div>\\n";
            return $error;
        }

        return false;
    }

    function save_file() {
        if (!current_user_can(\'edit_theme_options\'))
            wp_die(__(\'Easy there, homey. You don&#8217;t have admin privileges to access theme options.\', \'wpcfe\'));

        $custom_editor = new wpcfe_custom_editor;

        if (isset($_POST[\'custom_file_submit\'])) {
            check_admin_referer(\'wpcfe-custom-file\', \'_wpnonce-wpcfe-custom-file\');
            $contents = stripslashes($_POST[\'newcontent\']); // Get new custom content
            $file = $_POST[\'file\']; // Which file?
            $allowed_files = $custom_editor->get_custom_files(); // Get list of allowed files

            if (!in_array($file, $allowed_files)) // Is the file allowed? If not, get outta here!
                wp_die(__(\'You have attempted to modify an ineligible file. Only files within the <code>/\'.WPCFE_CUSTOM_FOLDER.\'</code> folder may be modified via this interface. Thank you.\', \'wpcfe\'));

            $file_open = fopen(WPCFE_CUSTOM_FOLDER . \'/\' . $file, \'w+\'); // Open the file

            if ($file_open !== false) // If possible, write new custom file
                fwrite($file_open, $contents);

            fclose($file_open); // Close the file
            $updated = \'&updated=true\'; // Display updated message
        }
        elseif (isset($_POST[\'custom_file_jump\'])) {
            check_admin_referer(\'wpcfe-custom-file-jump\', \'_wpnonce-wpcfe-custom-file-jump\');
            $file = $_POST[\'custom_files\'];
            $updated = \'\';
        }

        wp_redirect(admin_url("admin.php?page=wpcfe-file-editor$updated&file=$file"));
    }

    function options_page() {
        global $wpcfe_site, $custom_files_array;

        $custom_editor = new wpcfe_custom_editor($custom_files_array);
?>

<div id="wpcfe_options" class="wrap<?php if (get_bloginfo(\'text_direction\') == \'rtl\') { echo \' rtl\'; } ?>">

<?php

    wpcfe_options_status_check();

    // Determine which file we\'re editing. Default to something harmless, like custom.css.
    $file = ($_GET[\'file\']) ? $_GET[\'file\'] : \'custom.css\';
    $files = $custom_editor->get_custom_files();
    $extension = substr($file, strrpos($file, \'.\'));

    // Determine if the custom file exists and is writable. Otherwise, this page is useless.
    $error = $custom_editor->is_custom_writable($file, $files);

    if ($error)
        echo $error;
    else {
        // Get contents of custom.css
        if (filesize(WPCFE_CUSTOM_FOLDER . \'/\' . $file) > 0) {
            $content = fopen(WPCFE_CUSTOM_FOLDER . \'/\' . $file, \'r\');
            $content = fread($content, filesize(WPCFE_CUSTOM_FOLDER . \'/\' . $file));
            $content = htmlspecialchars($content);
        }
        else
            $content = \'\';
    }
?>
    <div class="one_col">
        <form method="post" id="file-jump" name="file-jump" action="<?php echo admin_url(\'admin-post.php?action=wpcfe_file_editor\'); ?>">
            <h3><?php printf(__(\'Currently editing: <code>%s</code>\', \'wpcfe\'), "$file"); ?></h3>
            <p>
                <select id="custom_files" name="custom_files">
                    <option value="<?php echo $file; ?>"><?php echo $file; ?></option>
<?php
        foreach ($files as $f) // An option for each available file
            if ($f != $file) echo "\\t\\t\\t\\t\\t<option value=\\"$f\\">$f</option>\\n";
?>
                </select>
                <?php wp_nonce_field(\'wpcfe-custom-file-jump\', \'_wpnonce-wpcfe-custom-file-jump\'); ?>
                <input type="submit" id="custom_file_jump" class="button" name="custom_file_jump" value="<?php _e(\'Edit selected file\', \'wpcfe\'); ?>" />
            </p>
<?php
        if ($extension == \'.php\')
            echo "\\t\\t\\t<p class=\\"alert\\">" . __(\'<strong>Note:</strong> If you make a mistake in your code while modifying a <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> file, saving this page <em>may</em> result your site becoming temporarily unusable. Prior to editing such files, be sure to have access to the file via <acronym title="File Transfer Protocol">FTP</acronym> or other means so that you can correct the error.\', \'wpcfe\') . "</p>\\n";
?>
        </form>
        <form class="file_editor" method="post" id="template" name="template" action="<?php echo admin_url(\'admin-post.php?action=wpcfe_file_editor\'); ?>">
            <input type="hidden" id="file" name="file" value="<?php echo $file; ?>" />
            <p><textarea id="newcontent" name="newcontent" rows="25" cols="50" class="large-text"><?php echo $content; ?></textarea></p>
            <p>
                <?php wp_nonce_field(\'wpcfe-custom-file\', \'_wpnonce-wpcfe-custom-file\'); ?>
                <input type="submit" class="button-primary" id="submit" name="custom_file_submit" value="<?php _e(\'Save changes\', \'wpcfe\'); ?>" />
            </p>
        </form>
    </div>

</div>
<?php
    }
}

// Update message after saving the changes
function wpcfe_options_status_check($depth = 1) {
    $indent = str_repeat("\\t", $depth);

    if ($_GET[\'updated\']) {
        echo "$indent<div id=\\"updated\\" class=\\"updated fade\\">\\n";
        echo "$indent\\t<p>" . __(\'Options updated!\', \'wpcfe\') . \' <a href="\' . get_bloginfo(\'url\') . \'/">\' . __(\'Check out your site &rarr;\', \'wpcfe\') . "</a></p>\\n";
        echo "$indent</div>\\n";
    }

}

结束

相关推荐

the_editor() function

我正在编写一个需要用户输入的WordPress插件,所以我想我应该尝试使用默认的WordPress富文本编辑器。首先是为了在WordPress已经有文本编辑器的情况下,不必在我的插件中包含文本编辑器,其次是为了让用户更无缝。实际上,我只想要默认编辑器,没有任何更改或定制。问题是,我无法让它正常工作,而且the_editor() 作用目前,我的文本框应该是:<?php the_editor( $content, $id = \'content\', $prev_id = \'title\'