そうだ、奥多摩に行こう

続・WordPressの自動整形を解除するプラグイン

2007年7月14日

pre要素の中はpとかbrが付かないので、script要素も同じルールを適用すればいいかも、と思いformatting.phpのfunction wpautop()の所を見る。80~81行目に

if (strpos($pee, '<pre') !== false)        $pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', &quot; stripslashes('$1') .  stripslashes(clean_pre('$2'))  . '</pre>' &quot;, $pee);`</pre>

という部分があったので、ここのpreをscriptに書き換えたものを追記したらうまくいった。
このままだとWPのバージョンアップの時に上書きされてしまうので、昨日作ったプラグインの中に入れてみた。動作の流れがよく分からないけど、とりあえずうまく動く。

<pre>`<?php/*Plugin Name: ScriptShieldPlugin URI: http:/hogehoge.com/Description: stop wpautop in ScriptTags.Version: 0.1Author: oreAuthor URI: http:/hogehoge.com/*/$scriptshield = new ScriptShield();add_filter('the_content', array(&amp;$scriptshield, 'delete_paragraph'));add_filter('the_excerpt', array(&amp;$scriptshield, 'delete_paragraph'));unset($scriptshield);class ScriptShield {        function delete_paragraph($content) {                $content = preg_replace('|<p><script([^>]*)>|i', &quot;<script$1>&quot;, $content);                $content = str_replace('</script></p>', '</script>', $content);                if (strpos($content, '<script') !== false)                        $content = preg_replace('!(<script.*?>)(.*?)</script>!ise', &quot; stripslashes('$1') .  stripslashes(clean_pre('$2'))  . '</script>' &quot;, $content);                                return $content;        }        }?>

BBcodeもどきを使うと、プラグインを使わなくなったときにゴミになるし嫌だな…と思っていたので、とりあえずこれを使ってみることにします。
本当はデフォルトで対応してくれてると助かるんだけど、scriptの中にpを入れる理由が何かあるんだろうか。その辺がわからないから、その内地雷踏みそうで怖い。それみたことかーッ!と誰かに言われそうだ。