無限の数のテンプレートを作成する以外に、WordPressの投稿でJavaScriptを使用するための最も信頼のおけるベストプラクティスは何ですか?
それをHTMLタブに配置すると、 WordPressが挿入を要求するタグp
とタグが原因で、多くの問題が発生します。br
他の誰かがこの問題に遭遇しますか?
前もって感謝します!
無限の数のテンプレートを作成する以外に、WordPressの投稿でJavaScriptを使用するための最も信頼のおけるベストプラクティスは何ですか?
それをHTMLタブに配置すると、 WordPressが挿入を要求するタグp
とタグが原因で、多くの問題が発生します。br
他の誰かがこの問題に遭遇しますか?
前もって感謝します!
空白を取り除き、JSに戻ると、機能します。これでは、WPエディターにブレークを追加して戻るものは何も与えられません。
または、functions.phpにこれをスローします。
// <!-- noformat on --> and <!-- noformat off --> functions
function newautop($text)
{
$newtext = "";
$pos = 0;
$tags = array('<!-- noformat on -->', '<!-- noformat off -->');
$status = 0;
while (!(($newpos = strpos($text, $tags[$status], $pos)) === FALSE))
{
$sub = substr($text, $pos, $newpos-$pos);
if ($status)
$newtext .= $sub;
else
$newtext .= convert_chars(wptexturize(wpautop($sub))); //Apply both functions (faster)
$pos = $newpos+strlen($tags[$status]);
$status = $status?0:1;
}
$sub = substr($text, $pos, strlen($text)-$pos);
if ($status)
$newtext .= $sub;
else
$newtext .= convert_chars(wptexturize(wpautop($sub))); //Apply both functions (faster)
//To remove the tags
$newtext = str_replace($tags[0], "", $newtext);
$newtext = str_replace($tags[1], "", $newtext);
return $newtext;
}
function newtexturize($text)
{
return $text;
}
function new_convert_chars($text)
{
return $text;
}
remove_filter('the_content', 'wpautop');
add_filter('the_content', 'newautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'newtexturize');
remove_filter('the_content', 'convert_chars');
add_filter('the_content', 'new_convert_chars');
次に、次のようにエディターでJSを括弧で囲みます。
<!-- noformat on -->
<script type="text/javascript">
blah blah blah
</script>
<!-- noformat off -->
これを可能にするハックがいくつかありますが、最も簡単な方法は、個々の投稿にJavaScriptを追加できるプラグインを使用することです。グーグルはこれを引き上げたが、もっとあると確信している。
<script>
または、ページのURLに基づいてヘッダーにタグを挿入する「シェル」JavaScriptファイルを作成することもできます。URLが。のtestFile
名前のページに名前の付いたスクリプトファイルを追加するとします。次のコードを次のシェルファイルに入れます。testPage
www.mydomain.com/testPage
shell.js
if (location.href === "http://www.mydomain.com/testPage") {
var testFile = document.createElement("script");
testFile.setAttribute("src","testFile.js");
document.getElementsByTagName("head")[0].appendChild(testFile);
}
shell.js
カスタムJavaScriptを使用して、ページごとに個別のスクリプトインジェクションコードスニペットを追加する必要があります。
<script src='shell.js'></script>
次に、 WordPressの組み込みエディターを使用してヘッダーテンプレートに追加します。
Wordpressにもいくつかの公式の推奨事項があります。