Skip to content

Instantly share code, notes, and snippets.

@oppara
Created December 11, 2019 05:22
Show Gist options
  • Save oppara/abac3dde9aa2ad7d902208eadaca995a to your computer and use it in GitHub Desktop.
Save oppara/abac3dde9aa2ad7d902208eadaca995a to your computer and use it in GitHub Desktop.
WordPress ビジュアルエディタ対応
<?php
// テーマの function.php あたりに追加
// もしくは、プラグインのコンスタラクタあたりに追加
// 管理側固定ページエディター自動挿入のPタグBRタグ削除
// オートフォーマット関連の無効化
add_action('init', function() {
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
});
// mainタグや一部の属性が削除されてしまう場合の対応
add_filter('wp_kses_allowed_html', function($tags, $context) {
switch ($context) {
case 'post':
// $tags['有効にしたいタグ'] = [
// '有効にしたい属性' => true,
// ];
// mainタグを有効にする
$tags['main'] = [
'id' => true,
'class' => true,
'role' => true,
];
$attrs = [
'itemprop',
'itemscope',
'itemtype',
];
// 各タグのパンくず関連の属性を有効にする
foreach ($attrs as $attr) {
$tags['a'][$attr] = true;
$tags['nav'][$attr] = true;
$tags['span'][$attr] = true;
$tags['ul'][$attr] = true;
$tags['li'][$attr] = true;
$tags['meta'][$attr] = true;
}
break;
}
return $tags;
});
// ビジュアルモードとテキストモードを切り替え対応
// TinyMCEの設定
add_filter('tiny_mce_before_init', function($init) {
global $allowedposttags;
// すべてのタグを許可する
$init['valid_elements'] = '*[*]';
$init['extended_valid_elements'] = '*[*]';
// aタグ内にすべてのタグを含められるようにする
// i.e. <a href="#"><h1>header</h1></a>
$init['valid_children'] = '+a[' . implode('|', array_keys($allowedposttags)) . ']';
// bodyタグ内にmeta, styleタグを含められるようにする
// liタグ内にmetaタグを含められるようにする
// mateタグに設定できる属性を設定
$init['valid_children'] .= ',+body[meta|style],+li[meta],+meta[itemprop|content]';
// javascriptから始まるurlを有効にする
$init['allow_script_urls'] = true;
// 改行が<p>にならないようにする
$init['forced_root_block'] = '';
// 検索してよく出てくる以下の設定は、TinyMCEv4以降は無効
// https://www.tiny.cloud/docs-4x/
// // 不正確なhtmlを修正しない -> TinyMCEv4以降は回避不可能
// $init['verify_html'] = false;
// $init['apply_source_formatting'] = false;
// // 改行が<p>にならないようにする -> forced_root_blockを使う
// $init['force_br_newlines'] = true;
// $init['force_p_newlines'] = false;
// // インデントを有効にする
// $init['indent'] = true;
// // テキストやインライン要素を自動的にpタグで囲む機能を無効にする
// $init['wpautop'] = false;
return $init;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment