WordPress’s visual content editor (TinyMCE) is one of many elements that contribute to the power of its content management capabilities. You can tailor this tool to be even more powerful both in its capabilities as they relate to your content. This presentation will cover adding custom stylesheets based on post type to the editor, modifying and removing buttons from the editor, and even creating your own custom buttons for the TinyMCE toolbar. It even covers brand new WordPress 3.3 techniques introduced with the new wp_editor function.
Intended for developers, this introduces new ideas and techniques to even the most experienced programmers, while also providing newer developers a good sense of how to use WordPress hooks in creative and powerful ways.
This was presented to developers at several WordCamps (WordPress conferences) around the country, notably the 2011 WordCamps for Chicago, Philly, and Orlando.
2. About Me: Jake Goldman
• President (& chief engineer!) @ 10up LLC, a
WordPress development & strategy agency
• Author of over a dozen WordPress plug-ins
• Dozens of clients, from university like Bates
College to WP.com VIP clients like TechCrunch
• Writer/expert reviewer @ Smashing Magazine
• @jakemgold
Editing the Visual Editor
4. Hypothesis #1
The styling of content in your editor should at
least somewhat match the styling of content on
your page.
!=
Editing the Visual Editor
6. Hypothesis #3
WordPress does a pretty good job of knowing
how to clean up our authors’ messes, but we
know our authors even better.
Editing the Visual Editor
7. Hypothesis #4
Sometimes we need more ways to style content
than we currently have.
How do I apply a
“specifications” style??
Editing the Visual Editor
8. Hypothesis #5
Sometimes we need to put editor-like content
in more than one place.
Editing the Visual Editor
9. Hypothesis #6
“Shortcodes” alone are not intuitive solutions
for special features, and are not discoverable.
Does this guy
expect me to
remember
this?
Editing the Visual Editor
11. Solution #1: Style Your Editor
( functions.php )
add_action( ‘after_theme_setup’, ‘eve_theme_setup’ );
function eve_theme_setup() {
add_editor_style();
}
Editing the Visual Editor
12. Solution #1: Style Your Editor
( functions.php )
add_action( ‘after_theme_setup’, ‘eve_theme_setup’ );
function eve_theme_setup() {
add_editor_style();
}
Hook is non-essential,
but good form.
Editing the Visual Editor
13. Solution #1: Style Your Editor
html .mceContentBody { max-width:550px; }
body.mceContentBody {
font-family: 'lucida grande',sans-serif; color: #555;
}
p,ul,ol,h4,h5,h6 {
line-height:20px; margin: 0 0 20px 0; font-size:13px;
}
...
Editing the Visual Editor
14. Solution #1: Style Your Editor
Tip: matching the editor body width to the
content width on the front end makes a big
difference in designing content!
html .mceContentBody { max-width:550px; }
body.mceContentBody {
font-family: 'lucida grande',sans-serif; color: #555;
}
p,ul,ol,h4,h5,h6 {
line-height:20px; margin: 0 0 20px 0; font-size:13px;
}
...
Editing the Visual Editor
15. Solution #2: Style Based on Post Type
( functions.php )
add_action( 'do_meta_boxes', 'eve_setup_other_stylesheets' );
function eve_setup_other_stylesheets( $post_type ) {
if ( $post_type == 'page' ) {
remove_editor_styles();
add_editor_style( 'editor-style-page.css' );
}
remove_action('do_meta_boxes','eve_setup_other_stylesheets‘);
}
Editing the Visual Editor
16. Solution #2: Style Based on Post Type
( functions.php )
add_action( 'do_meta_boxes', 'eve_setup_other_stylesheets' );
function eve_setup_other_stylesheets( $post_type ) {
if ( $post_type == 'page' ) {
remove_editor_styles();
add_editor_style( 'editor-style-page.css' );
}
Note #1: We don’t have
remove_action('do_meta_boxes','eve_setup_other_stylesheets‘);
} to use this hook. But it’s
pretty convenient.
Editing the Visual Editor
17. Solution #2: Style Based on Post Type
( functions.php )
add_action( 'do_meta_boxes', 'eve_setup_other_stylesheets' );
function eve_setup_other_stylesheets( $post_type ) {
if ( $post_type == 'page' ) {
remove_editor_styles();
add_editor_style( 'editor-style-page.css' );
}
remove_action('do_meta_boxes','eve_setup_other_stylesheets‘);
}
Note #2: We don’t have to remove styles.
We can add multiple editor styles.
Editing the Visual Editor
18. Solution #2: Style Based on Post Type
( functions.php )
add_action( 'do_meta_boxes', 'eve_setup_other_stylesheets' );
function eve_setup_other_stylesheets( $post_type ) {
if ( $post_type == 'page' ) {
remove_editor_styles();
add_editor_style( 'editor-style-page.css' );
} Note #3: This concept
applies to all our
remove_action('do_meta_boxes','eve_setup_other_stylesheets‘);
} TinyMCE / editor tips!
Editing the Visual Editor
20. Solution #3: Modify Allowed Post Tags
( functions.php )
add_action( 'init', 'eve_modify_allowed_post_tags' );
function eve_modify_allowed_post_tags() {
global $allowedposttags;
unset( $allowedposttags['blockquote'] );
}
Editing the Visual Editor
21. Solution #3: Modify Allowed Post Tags
Don’t forget!
( functions.php )
Users with unfiltered_html capabilities
add_action( 'init', 'eve_modify_allowed_post_tags' );
(editors & admins) are not filtered by this. If
function eve_modify_allowed_post_tags() {
global $allowedposttags; rid all users of this element,
you really want to
unset( $allowedposttags['blockquote'] );
} you’ll need to remove that capability from the
role or filter content elsewhere.
Editing the Visual Editor
31. Power of wp_editor()
'wpautop' => true, // use wpautop?
'media_buttons' => true, // show insert/upload button(s)
'textarea_name' => $editor_id, // set the textarea name to
something different, square brackets [] can be used here
'textarea_rows' => get_option('default_post_edit_rows', 10), //
rows="..."
'tabindex' => '',
'editor_css' => '', // intended for extra styles for both visual
and HTML editors buttons, needs to include the <style> tags, can
use "scoped".
'editor_class' => '', // add extra class(es) to the editor textarea
'teeny' => false, // output the minimal editor config used in Press
This
'dfw' => false, // replace the default fullscreen with DFW (needs
specific DOM elements and css)
'tinymce' => true, // load TinyMCE, can be used to pass settings
directly to TinyMCE using an array()
'quicktags' => true // load Quicktags, can be used to pass settings
directly to Quicktags using an array()
Editing the Visual Editor
( wp-includes/class-wp-editor.php )
32. Power of wp_editor()
'wpautop' => true, // use wpautop?
'media_buttons' => true, // show insert/upload button(s)
'textarea_name' => $editor_id, // set the textarea name to
something different, square brackets [] can be used here
'textarea_rows' => get_option('default_post_edit_rows', 10), //
rows="..."
'tabindex' => '',
'editor_css' => '', // intended for extra styles for both visual
Whoa
and HTML editors buttons, needs to include the <style> tags, can
use "scoped".
'editor_class' => '', // add extra class(es) to the editor textarea
'teeny' => false, // output the minimal editor config used in Press
This
'dfw' => false, // replace the default fullscreen with DFW (needs
specific DOM elements and css)
'tinymce' => true, // load TinyMCE, can be used to pass settings
directly to TinyMCE using an array()
'quicktags' => true // load Quicktags, can be used to pass settings
directly to Quicktags using an array()
Editing the Visual Editor
( wp-includes/class-wp-editor.php )
33. Solution #7: A button for your
shortcode
My button!
Editing the Visual Editor
34. Solution #7: A button for your
shortcode
Step 1: Make your TinyMCE plug-in
Editing the Visual Editor
37. Solution #7: A button for your
shortcode
Step 2: Register your plug-in with TinyMCE ( functions.php )
add_filter('mce_external_plugins','eve_add_mail_shortcode_plugin');
function eve_add_mail_shortcode_plugin( $plugin_array ) {
$plugin_array['eve_mail'] = get_stylesheet_directory_uri() .
'/editor_buttons/editor_plugin.js';
return $plugin_array;
}
Editing the Visual Editor
38. Solution #7: A button for your
shortcode
Step 3: Add your button to the editor ( functions.php )
add_filter( 'mce_buttons', 'even_add_mail_shortcode_button');
function even_add_mail_shortcode_button( $buttons ) {
array_push( $buttons, "|", "eve_mail" );
return $buttons;
}
Editing the Visual Editor
39. Solution #7: A button for your
shortcode
Editing the Visual Editor
40. Power Tip: Style the fullscreen editor
Editing the Visual Editor
41. Power Tip: Style the fullscreen editor
( functions.php )
add_action( 'admin_print_styles-post-new.php',
'eve_fullscreen_styles' );
add_action('admin_print_styles-post.php','eve_fullscreen_styles');
function eve_fullscreen_styles() { ?>
<style type="text/css">
.fullscreen-active #wp-fullscreen-title { font-family: Georgia;
font-size: 40px; }
</style>
<?php }
Editing the Visual Editor
42. Bonus Tip: Default Content
Remember: can be post type specific! ( functions.php )
add_filter( 'default_content', ‘eve_editor_content' );
function my_editor_content( $content ) {
$content = “<h2>Subtitle</h2>nnStart writing!";
return $content;
}
Editing the Visual Editor
43. Editing the Visual Editor
by Jake Goldman
@jakemgold
slides & code will be available at
get10up.com
Editing the Visual Editor