Archives

Disable WYSIWYG Editor For Custom Post Types

If you need to make sure that your custom post type doesn’t have the ‘visual’ tab, you can easily diable it. Suppose you have a custom post type named ‘movie’:

add_filter( 'user_can_richedit', 'disable_for_cpt' );
function disable_for_cpt( $default ) {
	global $post;
	if ( 'movie' == get_post_type( $post ) )
		return false;
	return $default;
}

Add Font-Select Drop Down to Visual Editor

Add the font-select menu to the 2nd row of buttons (revealed with the Kitchen Sink button)

If some items aren’t removed from the second row, the buttons won’t fit nicely on a 1024×768 screen.

See WordPress’s default buttons in wp_tiny_mce()

add_filter('mce_buttons_2', 'add_fontselect_row_2' );

function add_fontselect_row_2( $mce_buttons ) {
    /*
        find the keys of the buttons to be sacrificed
    */
    $pastetext = array_search( 'pastetext', $mce_buttons );
    $pasteword = array_search( 'pasteword', $mce_buttons );
    $removeformat = array_search( 'removeformat', $mce_buttons );

    unset( $mce_buttons[ $pastetext ] );
    unset( $mce_buttons[ $pasteword ] );
    unset( $mce_buttons[ $removeformat ] );

    /*
        insert the new dropdown where the sacrificed buttons used to be
        alter as needed
    */
    array_splice( $mce_buttons, $pastetext, 0, 'fontselect' );
    return $mce_buttons;
}

But if you’re adding the 3rd row, you’re probably clear (unless you have other plugins that are adding buttons).

add_filter('mce_buttons_3', 'add_fontselect_row_3' );

function add_fontselect_row_3( $mce_buttons ) {
    $mce_buttons[] = 'fontselect';
    return $mce_buttons;
}

Currently, any buttons added to the 3rd or 4th rows will always be visible (Kitchen Sink only shows/hides the 2nd row) – but a patch has been submitted to correct this, hopefully it’ll be committed.

However, if you’ll probably want to clean up the font options – remove fonts that you’ll never use (or should never be used).

add_filter('tiny_mce_before_init', 'restrict_font_choices' );
function restrict_font_choices( $initArray ) {
    $initArray['theme_advanced_fonts'] = 
        'Andale Mono=andale mono,times;'.
        'Arial=arial,helvetica,sans-serif;'.
        //'Arial Black=arial black,avant garde;'.
        'Book Antiqua=book antiqua,palatino;'.
        //'Comic Sans MS=comic sans ms,sans-serif;'.
        'Courier New=courier new,courier;'.
        'Georgia=georgia,palatino;'.
        'Helvetica=helvetica;'.
        //'Impact=impact,chicago;'.
        //'Symbol=symbol;'.
        'Tahoma=tahoma,arial,helvetica,sans-serif;'.
        'Terminal=terminal,monaco;'.
        'Times New Roman=times new roman,times;'.
        'Trebuchet MS=trebuchet ms,geneva;'.
        'Verdana=verdana,geneva;'.
        //'Webdings=webdings;'.
        //'Wingdings=wingdings,zapf dingbats'.
        '';
    return $initArray;
}

Force Default Editor Mode

Normally, WordPress will remember which editor mode you were in (Visual or HTML). So if you were using the HTML editor and you save a page, you’ll be returned to the HTML editor.

If you (or your clients) would benefit from always returning to particular mode, you can set that up

add_filter( 'wp_default_editor', 'force_default_editor' );
function force_default_editor() { 
	//allowed: tinymce, html, test
	return 'tinymce';
}

*Functionality is there but quirky in pre-3.3

Remove Buttons From Distraction-Free Writing

Want even fewer distractions in DFW? Remove some of the buttons

add_filter( 'wp_fullscreen_buttons', 'remove_fullscreen_buttons' );
function remove_fullscreen_buttons( $buttons ) {
	unset($buttons['bullist']);
	unset($buttons['numlist']);
	unset($buttons['1']);
	return $buttons;
}

Here are the buttons you have to work with:

$buttons = array(
	// format: title, onclick, show in both editors
	'bold' => array( 'title' => __('Bold (Ctrl + B)'), 'onclick' => 'fullscreen.b();', 'both' => false ),
	'italic' => array( 'title' => __('Italic (Ctrl + I)'), 'onclick' => 'fullscreen.i();', 'both' => false ),
	'0' => 'separator',
	'bullist' => array( 'title' => __('Unordered list (Alt + Shift + U)'), 'onclick' => 'fullscreen.ul();', 'both' => false ),
	'numlist' => array( 'title' => __('Ordered list (Alt + Shift + O)'), 'onclick' => 'fullscreen.ol();', 'both' => false ),
	'1' => 'separator',
	'blockquote' => array( 'title' => __('Blockquote (Alt+Shift+Q)'), 'onclick' => 'fullscreen.blockquote();', 'both' => false ),
	'image' => array( 'title' => __('Insert/edit image (Alt + Shift + M)'), 'onclick' => "fullscreen.medialib();", 'both' => true ),
	'2' => 'separator',
	'link' => array( 'title' => __('Insert/edit link (Alt + Shift + A)'), 'onclick' => 'fullscreen.link();', 'both' => true ),
	'unlink' => array( 'title' => __('Unlink (Alt + Shift + S)'), 'onclick' => 'fullscreen.unlink();', 'both' => false ),
	'3' => 'separator',
	'help' => array( 'title' => __('Help (Alt + Shift + H)'), 'onclick' => 'fullscreen.help();', 'both' => false )
);

Allow Non-Standard Tags in the Editor

If you find yourself needing to add non-standard tags to the WordPress editor, you may also find yourself feeling very frustrated. With just a little bit of code, you can tell the editor to accept extra tags

add_filter( 'tiny_mce_before_init', 'mce_extended_valid_elements', 10, 2);
function mce_extended_valid_elements( $mceInit, $editor_id ) {
	//allow only the basic tag
	//$mceInit['extended_valid_elements'] .= ',sometag';
	//allow the tag and a attribute
	$mceInit['extended_valid_elements'] .= ',sometag[someattribute]';
	//allow the tag and multiple attribute
	//$mceInit['extended_valid_elements'] .= ',sometag[someattribute|anotherattribute]';
	//allow the tag and any attribute
	//$mceInit['extended_valid_elements'] .= ',sometag[*]';
	return $mceInit;
}

Limit Spellchecker Languages

If you don’t want your spellchecker dropdown to be too big, you can limit the available languages. Cut it down to only a few, or even just one.

add_filter('mce_spellchecker_languages', 'limit_spellchecker_languages');
function limit_spellchecker_languages() {
	//original: +English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv
	return '+English=en';
}

Change Auto-Save Frequency

By default, WordPress will save an auto-draft for you, and will update it every 60 seconds. If that’s too frequent, or not enough you can update it.

Save every 15 seconds

define('AUTOSAVE_INTERVAL', 15);

Save every 5 minutes

define('AUTOSAVE_INTERVAL', 60*5);