2016-02-18 147 views
4

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

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

此时,我编辑微型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'); 

我不想加入我的选项页面中的所有代码进行卷积的帖子,但我可能需要就如何在传递方式的一些方向变量作为$ in []中的值。如前所述,变量将在选项页面中创建并保存,更新微小的mce函数。

我研究了很多,我找不到任何关于此的直接信息 - 像往常一样,我不是在寻找某人来执行我的代码,而是可能会将我推向正确的方向。

谢谢!

编辑新建CODE

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'); 
?> 

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

+0

将它们保存到选项表中,在过滤器中检索它们。 –

+0

谢谢你的快速回答!你能详细说明一下细节吗?我应该使用apply_filter()吗?谢谢 ! – mmarquez

回答

0

在您的选项页面上,您可以使用update_option保存选项。然后,在您的my_format_TinyMCE功能中,您可以使用get_option访问它们。

+0

谢谢@Flyer--这种方法更有意义,但我遇到了困难。我正在更新我的代码以显示完整的上下文。谢谢! – mmarquez

+0

@mmarquez我认为你没有正确地启动设置的东西。有关信息和示例,请参阅[设置API](https://developer.wordpress.org/plugins/settings/settings-api/) –

+0

问题是试图使用$ variable = get_option(),但如果我只使用get_option()作为变量,它工作正常。你的回答是正确的 - 你的链接非常有用。谢谢! – mmarquez