【问题标题】:Editing function.php from a custom plugin in WordPress. Line of code prohibiting certain core functions?从 WordPress 中的自定义插件编辑 function.php。禁止某些核心功能的代码行?
【发布时间】:2016-12-14 23:07:48
【问题描述】:

我对编码很陌生,我刚刚接管了一个 WordPress 网站,开发人员在该网站上设置了自己的插件来编辑主题之外的 functions.php 文件。似乎有一行代码破坏了 WordPress 中的某些核心功能,例如:安装新插件失败、无法搜索主题、媒体库未加载。但是如果我停用这个插件,所有这些功能都会恢复生机,但是网站布局的某些部分会在没有插件的情况下中断。那么谁能告诉我如何修复此代码?非常感谢你。

<?php
/* Your code goes below here. */

ob_start();
function check_user_logged_in(){

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
}
}

// Something is wrong with this next line of code. Can't find new themes or install new plugins. When removed everthing works ok.
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>';
}

//* Display a custom favicon
add_filter( 'genesis_pre_load_favicon', 'sp_favicon_filter' );
function sp_favicon_filter( $favicon_url ) {
return 'http://winningsportsplays.com/wspwp/wp-content/favicon.ico';
}

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

【问题讨论】:

    标签: javascript php jquery wordpress


    【解决方案1】:

    代码本身对我来说看起来不错。我的猜测是,通过“init”初始化函数会导致与某些核心文件发生冲突。

    尝试改变这个:

    add_action('init', 'check_user_logged_in');
    

    到这里:

    add_action('wp_head', 'check_user_logged_in');
    

    看看这是否能解决问题。

    【讨论】:

    • “init”肯定是问题所在,但您的建议破坏了页面上各部分的样式。问题似乎是该功能正在应用于管理或仪表板部分并破坏它。
    • 如果问题只是破坏了管理区域但在其他地方很好,那么按照您的建议使用 if (!is_admin()) {} 应该可以解决它。
    【解决方案2】:

    在代码中添加if (!is_admin()) {} 似乎有效,但是否正确?

    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
    }
    **}**
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多