WP(ワードプレス)で勝手にpタグやbrタグが削除されてしまう

みなさん、こんにちは。
今回は表題の件をご紹介します。

といってもWPの構築になれている人は『え、初期の初期じゃない?』って思う人もいるかもしれませんが
今回私が表題のことをやりたかったのは以下の条件のもとでした。

・すでに公開されているサイト
・もともとは別の人が構築したサイトで、管理更新のみ任されている
・大量の記事が入っている
・最新版ではないワードプレス
・あとから新規で作成したページ以外は上記の現象が起こっていても問題ないので残しておきたい

というものです。

もともとお客様より既存のサイトに一つだけ異色の製品ページを設けたいという連絡があり
新規で作成して、おわっていたのですがことあるごとに部分部分でなぜかデザインの崩れが生じており
ご連絡をいただいておりました。

更新をしているかどうか尋ねると「文字だけなおしている」ということだったので
「???」と疑問に思いながらも調査すると勝手にpタグやbrタグが削除されていることに
気づきました。

ただ他のページは同じ現象が起こっていても、このままでデザインが成り立っているので
functionにもろもろ記載してすべての記事に適応されてるといろいろとまずい…という条件下の
もと対処法を探していました。

すると素敵なものを作成してくださっている方がいました~☆彡

ぺららぼさんがかいたブログに記事ごとに
管理画面から「整形」「無整形」を選択できるような仕組みを選択できます。

以下ソースです。上記ブログから引用させていただきました~!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//==============================================================================
//
//  自動整形を無効にするカスタムフィールドを作成
//
//==============================================================================
 
add_action(
    'add_meta_boxes',
    function(){
        $screens = array('post_type'=> 'products');
        foreach($screens as $scrn){
            add_meta_box(
                'peralab-custombox-dont-autoformatting',
                'PeralabDontAutoFormatting_CustomBoxCreate',
                $scrn,'side','default',null);
        }
    }
);
 
 
function PeralabDontAutoFormatting_CustomBoxCreate($post){
    $data_str = get_post_meta($post->ID, "dont_autoformat_radio", true);
    if($data_str != 'dont'){
        $data_str = 'format';
    }
    wp_nonce_field('action-noncekey-dontautoformat', 'noncename-dontautoformat');?>
    <div>
     
    <!-- 出力する文字列 -->
    <p><label><input name="name-metabox_autoformat_radio" type="radio" value="format" <?php echo (($data_str == 'format') ? 'checked' : '') ?>>整形する(初期値)</label></p>
    <p><label><input name="name-metabox_autoformat_radio" type="radio" value="dont" <?php echo (($data_str == 'dont') ? 'checked' : '') ?>>整形しない</label></p>
    <p><label>ビジュアルエディタの整形無効の切り替えは[下書き保存] [更新]などで記事の保存後から反映されます。</label></p>
    </div>
    <?php }
 
//--------------------------------------------------------------
//  カスタムボックス内のフィールド値更新処理
//--------------------------------------------------------------
add_action(
    'save_post',
    function($post_id){
        //nonceを確認
        if(isset($_POST['noncename-dontautoformat']) == false
                || wp_verify_nonce($_POST['noncename-dontautoformat'], 'action-noncekey-dontautoformat') == false) {
            return;
        }
        if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
            return;
        }
        if(isset($_POST['post_type'])){
            if($_POST['post_type'] == 'page'){
                if(!current_user_can('edit_page', $post_id)){return;}
            }
            else{
            if(!current_user_can('edit_post', $post_id)){return;}
            }
        }
 
        if(isset($_POST['name-metabox_autoformat_radio'])){
            update_post_meta($post_id, "dont_autoformat_radio", $_POST['name-metabox_autoformat_radio']);
        }
    }
);
 
//=========================
//  自動整形無効の実処理
//=========================
 
//記事表示時の整形無効
add_action(
    'wp_head',
    function(){
        if(get_post_meta(get_the_ID(), 'dont_autoformat_radio', true) == 'dont'){
            remove_filter('the_content', 'wpautop');
            remove_filter('the_excerpt', 'wpautop');
        }
    }
);
 
add_filter(
    'tiny_mce_before_init',
    function($init_array){
        if(get_post_meta(get_the_ID(), 'dont_autoformat_radio', true) == 'dont'){
            global $allowedposttags;
            $init_array['valid_elements']          = '*[*]';
            $init_array['extended_valid_elements'] = '*[*]';
            $init_array['valid_children']          = '+a[' . implode( '|', array_keys( $allowedposttags ) ) . ']';
            $init_array['indent']                  = true;
            $init_array['wpautop']                 = false;
            $init_array['force_p_newlines']        = false;
        }
        return $init_array;
    }
);

ちなみに一番上に記載してある以下の部分は固定ページ、デフォルトの投稿、カスタム投稿タイプで書き方が変わります。

固定ページの場合

1
$screens = array('page');

デフォルトの投稿の場合

1
$screens = array('post');

固定ページとデフォルトの投稿の場合

1
$screens = array('post','page');

カスタム投稿タイプの場合

1
$screens = array('post_type'=> 'カスタム投稿タイプ名');

以上です!ぺららぼさんありがとうござます!
みなさんも参考にしてみてください~