WordPress 4.8 テキストウィジェット

WordPress4.8でテキストウィジェットのテキストエリアは、エディタに置き換えられました。

この変更は多くの人にとって、便利な機能だと好意的に受け入れられると思います。

ただ、ウィジェットのp要素の自動補完チェックボックスをあえてチェックしないで使っていたユーザーは、
今後新しくウィジェットを追加する際、注意が必要です。

WordPress4.8では、このチェックボックスはありません。

エディタによる p 要素の補完やテキスチャライズが行われると、レイアウトが崩れたり、機能に影響が出るような場合は、フィルターなどで処理が必要になるかもしれません。

そのような場合、テキストウィジェットのコンテンツ用フィルタ widget_text_contentフィルタが使えます。

以下の例では、ウィジェットからの出力をフィルターを使って a 要素以外の要素を削除して表示するように変更しています。

WordPress4.8.1で「HTML Code Widget」というエディタなしのウィジェットが追加されるようです

今回の変更は、現在進行中の新投稿エディタと関連するステップの一つかもしれませんので、

リッチエディターが邪魔な場合は、新規にカスタマイザーフィールドを作成するとか、ウィジェットの作成も視野に、、、

カスタマイザー デフォルト – WordPress Snippet


add_filter('widget_text_content','my_widget_text_content');

function my_widget_text_content($content){
	
	return strip_tags( $content,'<a>');
}

参考

wp-includes/default-filters.php

add_filter( 'widget_text','balanceTags');
add_filter( 'widget_text_content', 'capital_P_dangit', 11 );
add_filter( 'widget_text_content', 'wptexturize');
add_filter( 'widget_text_content', 'convert_smilies', 20 );
add_filter( 'widget_text_content', 'wpautop' );

関連

Version 4.8 « WordPress Codex

Addition of TinyMCE to the Text Widget – Make WordPress Core

Addition of TinyMCE to the Text Widgetで説明されている。ボタン追加の実装例

add_action( 'admin_print_scripts-widgets.php', 'custom_button_for_widgets_text_editor' );

function custom_button_for_widgets_text_editor() {
	
	$script=<<<TEXT_EDITOR
jQuery( document ).on( 'tinymce-editor-setup', function( event, editor ) {
 editor.settings.toolbar1 += ',mybutton';
 editor.addButton( 'mybutton', {
 text: 'My button',
 icon: false,
 onclick: function () {
editor.insertContent( 'Text from my button' );
 }
 });
});
TEXT_EDITOR;
	
	wp_add_inline_script( 'editor', $script );
}
add_action( 'admin_print_scripts-widgets.php', 'custom_elements_blockquote_for_widgets_text_editor' );

function custom_elements_blockquote_for_widgets_text_editor() {
	$script=<<<TEXT_EDITOR
jQuery( document ).on( 'tinymce-editor-setup', function( event, editor ) {
 if ( editor.settings.toolbar1 && -1=== editor.settings.toolbar1.indexOf( 'blockquote' ) ) {
 editor.settings.toolbar1 += ',blockquote';
 }
});
TEXT_EDITOR;
	wp_add_inline_script( 'editor', $script );
}

[emulsion_relate_posts]