【问题标题】:add_action('init') in WordPress functions.php extension seems to be breaking Jetpack activation?WordPress functions.php 扩展中的 add_action('init') 似乎破坏了 Jetpack 激活?
【发布时间】:2016-12-15 19:33:20
【问题描述】:

我一直有这个插件的问题,另一个开发人员将代码注入到 WordPress 中的 function.php 文件中,而没有操纵主题中的那个。我添加了if (!is_admin()) { 行,它解决了仪表板控件的一些问题,但现在当我尝试连接到 wordpress.com 以激活 Jetpack 时,它会将我带到我网站的首页,并在我的网站正文中写入此错误消息:

未找到,错误 404 您要查找的页面已不存在。也许您可以返回该站点的主页,看看是否能找到您要查找的内容。或者,您可以尝试使用下面的搜索表单找到它。

如果我停用此插件,他们使 Jetpack 激活将起作用,但我想知道是什么导致了问题。很确定它与代码的add_action('init', 'check_user_logged_in'); 部分有关,但我不知道用什么替换它。我试过用 wp_head 替换 init ,但它破坏了我页面上另一个插件的样式。有任何想法吗?这是下面的代码。

<?php

/* Your code goes below here. */

ob_start();

function check_user_logged_in(){
  if (!is_admin()) {
    if ( is_user_logged_in() ) { ?>
      <style  type="text/css" media="screen">
      #theme-my-login-2 .widget-wrap .widget-title { display: block !important; }
      </style>
   <?php
    } else { ?>
      <style type="text/css" media="screen">
         table.sidebar_result{margin-top:-10px;}
      </style>
<?php 
    }
  }
}

add_action('init', 'check_user_logged_in');

/* Add Read More Link to Excerpts */
add_filter('excerpt_more', 'get_read_more_link');
add_filter( 'the_content_more_link', 'get_read_more_link' );

function get_read_more_link() {
    return '...&nbsp;<a href="' . get_permalink() . '">[Read&nbsp;More]</a>';
}

/* Your code goes above here. */
?>

【问题讨论】:

    标签: javascript php jquery wordpress jetpack


    【解决方案1】:

    函数check_user_logged_in() 在编写时考虑了很多愚蠢,因为它肯定会破坏任何 WordPress 网站,因为 CSS sn-ps 在 WordPress init 阶段(最早的加载阶段之一)打印到屏幕上)。

    这将破坏来自template_redirect 钩子的所有重定向并破坏视图。我可能不会相信这样一个插件的作者。

    但是,您使用 wp_head 操作添加此内容的方法非常正确(您也可以使用 wp_footer 操作)。这仍然是一种不好的方法,因为样式应该与 wp_register_style()wp_enqueue_style() 一起排队。

    如果 CSS 代码破坏了您的主题,您要么删除 add_action 行,要么更改 CSS 以满足您的需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多