WordPress tinyMCE 自定义插件

Wordpress tinyMCE Custom Plugin

我刚刚开始在 Wordpress 中开发插件。现在我有一个函数,我将它作为过滤器传递给 'tiny_mce_before_init' 并带有特定变量以更改按钮、添加自定义样式和其他类似的东西。

我正在构建一个选项页面,我想控制传递给 tinyMCE 函数的变量,这样用户就可以 select 显示哪些按钮以及添加为编辑器自定义样式表。

至此,我编辑微型mce的功能就很好用了!选项页面还保存数据、复选框和我需要的所有其他内容。

我唯一的问题是我不明白如何将存储在 "options.php" 中的变量传递给当前的 tinyMCE 函数。这是我的 functions.php 文件中的当前函数:

function my_format_TinyMCE( $in ) {
    //styles for the editor to provide better visual representation.
    $in['content_css'] = get_template_directory_uri() . "/build/styles/tiny-mce-editor.css";
    $in['block_formats'] = "Paragraph=p; Heading 1=h1; Heading 2=h2";
    $in['toolbar1'] = 'formatselect,bold,italic,underline,superscript,bullist,numlist,alignleft,aligncenter,alignright,link,unlink,spellchecker';
    $in['toolbar2'] = '';
    $in['toolbar3'] = '';
    $in['toolbar4'] = '';
    return $in;
}
add_filter( 'tiny_mce_before_init', 'my_format_TinyMCE' );

我不想通过添加我的选项页面的所有代码来使 post 复杂化,但我可能需要一些指导来了解如何将变量作为 $in[] 的值传递.如前所述,变量将在选项页面中创建并保存,更新微型 mce 函数。

我已经研究了很多,但我找不到任何关于此的直接信息——像往常一样,我不是在找人来编写我的代码,而是可能会把我推向正确的方向。

谢谢!

编辑新代码

add_action('admin_menu', 'my_cool_plugin_create_menu');

function my_cool_plugin_create_menu() {
    add_menu_page('My Cool Plugin Settings', 'Cool Settings', 'administrator', __FILE__, 'my_cool_plugin_settings_page' , plugins_url('/images/icon.png', __FILE__) );
    add_action( 'admin_init', 'register_my_cool_plugin_settings' );
}

function register_my_cool_plugin_settings() {
    //register our settings
    register_setting( 'my-cool-plugin-settings-group', 'new_option_name' );
}

function my_cool_plugin_settings_page() {
    ?>
    <div class="wrap">
        <h2>Your Plugin Name</h2>
        <form method="post" action="options.php">
            <?php settings_fields( 'my-cool-plugin-settings-group' ); ?>
            <?php do_settings_sections( 'my-cool-plugin-settings-group' ); ?>
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">New Option Name</th>
                    <td><input type="text" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td>
                </tr>

            <?php submit_button(); ?>
        </form>
    </div>
<?php }

function my_format_TinyMCE( $in ) {

    $toolbar = get_option('new_option_name');

    //styles for the editor to provide better visual representation.
    $in['content_css'] = get_template_directory_uri() . "/build/styles/tiny-mce-editor.css";
    $in['block_formats'] = "Paragraph=p; Heading 1=h1; Heading 2=h2";
    $in['toolbar1'] = $toolbar;
    $in['toolbar2'] = '';
    $in['toolbar3'] = '';
    $in['toolbar4'] = '';
    return $in;
}
    add_filter( 'tiny_mce_before_init', 'my_format_TinyMCE' );
?>

我仍然无法访问存储的变量并在函数中使用它们。有任何想法吗?

在您的选项页面上,您可以使用 update_option. Then, in your my_format_TinyMCE function, you can access them with get_option 保存选项。