【问题标题】:&& is breaking the page in WordPress&& 正在破坏 WordPress 中的页面
【发布时间】:2011-09-05 22:35:46
【问题描述】:

我将此添加到我的 WordPress page

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

&& 正在转向

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

我将它粘贴到 WordPress HTML 视图中,并确保它没问题,但 WordPress 一直显示它。如何解决这个问题?

【问题讨论】:

  • 你把这段JS代码放到帖子里了吗?还是您正在编辑主题的 header.php 文件?
  • 您希望在帖子中显示给用户,还是为您自己的网站执行?
  • 我实际上没有添加它...但我负责修复它
  • 我负责修复错误,但我最初没有将 js 放在帖子中....我正在考虑将其移出到外部 js 文件中
  • 如何将 javascript 文件上传到另一个服务并从那里将其导入 Wordpress?见stackoverflow.com/a/65674751/5802289

标签: javascript html wordpress


【解决方案1】:

确保您的页面上正在执行以下行(通过内联 PHP(您需要一个插件)或在“functions.php”中):

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_content', 'wptexturize' );
remove_filter( 'the_content', 'convert_chars' );

【讨论】:

    【解决方案2】:

    您需要禁用 WP 的自动格式化。即使在 html 编辑器中,WP 也会自动格式化,并且空格和换行符会破坏您的 javascript。

    使用这个插件http://wordpress.org/extend/plugins/wp-no-format/

    2015 年 4 月 8 日更新:插件已过时,但对我仍然有效。

    这也有效:将插件直接添加到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');
    

    【讨论】:

      【解决方案3】:

      另一种选择是创建shortcode。在此示例中,仅当短代码包含属性 xy 时才会打印,例如:[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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-09-05
        • 2015-07-05
        • 2012-12-28
        • 2018-03-06
        • 2023-04-11
        • 1970-01-01
        • 2015-01-23
        • 2023-03-14
        • 1970-01-01
        相关资源
        最近更新 更多