繁体   English   中英

如何阻止框架破坏者破坏WordPress定制程序页面?

[英]How do I stop my frame buster from breaking the WordPress Customizer page?

看一个简单的破坏者:

<script type="text/javascript"> 
    if (top.location != self.location) { 
        top.location = self.location.href; 
    } 
</script>

但是,它似乎工作得很好。 它会破坏WordPress中的定制程序管理页面,在您自定义某些设置时,会在框架中预览网站。 我该如何修改它以使其破坏帧...但不是那个。

也许您可以:

  1. 检查它是否在同一个域中,并且不会破坏这些帧?
  2. 使用某种正则表达式匹配检查特定的定制器URL?

我对这两种解决方案都很满意,但是我不知道如何实现它们。 任何帮助,将不胜感激。

您可以使用wp_customize查询arg和is_user_logged_in函数将JavaScript封装在if语句中,如下所示:

<?php if ( ! ( isset( $_GET[ 'wp_customize' ] ) && is_user_logged_in() ) ): ?>
<script type="text/javascript"> 
    if (top.location != self.location) { 
        top.location = self.location.href; 
    } 
</script>
<?php endif; ?>

因此,显然top.location有一个名为pathname的字段,其中包含不带域的URL。 通过检查该字段,我可以排除该特定的管理页面。

            <script type="text/javascript">
            function parentIsEvil(parent) {
                var html = null;
                try {
                  var doc = top.location.pathname;
                } catch(err){
                  // do nothing
                }
                console.log(doc);
                return(doc != "/wp-admin/customize.php");
            }
            console.log(canAccessParent());
            if (top.location != self.location && parentIsEvil()) { 
                top.location = self.location.href;
            }
        </script>';

更新:我添加了检查跨域并捕获任何错误的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM