WordPress tinyMCE Custom Plugin

I’m just getting into plugin development in WordPress. Right now I have a function that a I pass as a filter to the ‘tiny_mce_before_init’ with specific variables to change the buttons, add custom styling and other similar things.

I’m in the process of building a an Options Page which I would like to control the variables passed to the tinyMCE function, that way the user can select which buttons to display as well as add a custom stylesheet to the editor.

Read More

At this point, my function to edit the tiny mce works great! and the options page is also saving data, checkboxes and everything else I need.

My only issue is I’m not understanding how to pass the variables stored in the “options.php” to the current tinyMCE function. This is the current function in my functions.php file:

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

I don’t want to convolute the post by adding all the code of my Options Page, but I might need some direction on how to approach on passing variables as the value of the $in[]. As mentioned before, the variables would be created in an Options Page and saved, updating the tiny mce function.

I’ve researched a lot and I can’t find any direct information on this — as usual, I’m not looking for somebody to do my code but maybe to push me in the right direction.

Thanks!

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

I’m still having difficulty accessing the stored variables and using them in the function. Any ideas?

Related posts

1 comment

Comments are closed.