&& ломает страницу в WordPress - PullRequest
2 голосов
/ 01 июня 2011

Я добавил это на мою страницу WordPress

if (script.readyState && script.onload!==null){
    script.onreadystatechange= function () {
        if (this.readyState == 'complete') mce_preload_check();
    }
}

и && превращается в

if (script.readyState && script.onload!==null){

Я вставил это в HTML-представление WordPress и убедился, что все в порядке, но WordPress продолжает отображать это. Как решить эту проблему?

Ответы [ 3 ]

7 голосов
/ 01 июня 2011

Вам необходимо отключить автоформатирование WP. WP автоматически отформатирует даже в html-редакторе, а пробелы и разрывы строк нарушат ваш javascript.

Используйте этот плагин http://wordpress.org/extend/plugins/wp-no-format/

Обновление от 08.08.2015: плагин устарел, но у меня все еще работает.

Это также работает: добавьте плагин непосредственно в functions.php и заключите ваш javascript в теги <!-- noformat on --> и <!-- noformat off -->

Добавить в файл functions.php:

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');
0 голосов
/ 19 июля 2019

Это сообщение, которое объясняет, что сообщения и страницы WordPress делают с & s, и как использовать «значение кодовой точки Unicode (0026) как лучший способ обойти эту проблему».

http://news.mullerdigital.com/2014/09/11/prevent-ampersand-issues-javascript-wordpress-pages-posts/

0 голосов
/ 18 августа 2013

Другой вариант - сделать шорткод . В этом примере шорткод будет напечатан, только если он содержит атрибуты x и y, например: [myscript x="10" y="20"]. Я использую простой скрипт, который показывает диалоговое окно предупреждения JS со значениями атрибутов.

add_shortcode( 'myscript', 'sample_shortcode_so_6195635' );

function sample_shortcode_so_6195635( $atts, $content = null )
{   
    if( isset( $atts['x'] ) && isset( $atts['y'] ) )
    {
        $x = $atts['x'];
        $y = $atts['y'];

        // See: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
        $html = <<<HTML
        <button onclick="myalert()">Show Shortcode Atts</button>

        <script type="text/javascript">
        function myalert()
        {
            if( $x < 10 && $y < 20 )
                alert( 'X less than 10 and Y less than 20' );
            else
                alert( 'other' );
        }
        </script>   
HTML;
        return $html;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...