【问题标题】:Fatal error: Call to undefined function add_filter() in index.php in Wordpress致命错误:在 Wordpress 的 index.php 中调用未定义的函数 add_filter()
【发布时间】:2012-10-27 21:56:21
【问题描述】:

我想通过用户浏览器动态更改我的 wordpress 主题。所以我在网上找到了这段代码,并将其添加到我的 index.php 文件中

add_filter('template', 'switch_theme_by_browser');
add_filter('stylesheet', 'switch_theme_by_browser');

function switch_theme_by_browser($theme) {

    $browser = $_SERVER['HTTP_USER_AGENT'];

    if(preg_match('/MSIE/i',$browser) && !preg_match('/Opera/i',$browser))
    {
        $theme = "twentyeleven";
    }
    elseif(preg_match('/Firefox/i',$browser))
    {
        $theme = "twentyten";
    }
    elseif(preg_match('/Chrome/i',$browser))
    {
        $theme = "Boxoffice";
    }

    return $theme;
}

之后它显示“致命错误:在第 17 行的 /home/xxx/public_html/domain.com/index.php 中调用未定义函数 add_filter()”

据我所知,“add_filter()”应该是一个内置于 wordpress 的函数。

【问题讨论】:

  • 尝试将此添加到functions.php而不是index.php
  • 问题是,它应该是网站首先阅读的内容,因此它知道要加载什么模板。

标签: wordpress wordpress-theming fatal-error add-filter


【解决方案1】:

正如 Dawson 所说,这需要放入您的 /wp-content/themes/[theme]/functions.php 文件中。

【讨论】:

  • 好的,现在它说:警告:需要(/home/xxx/public_html/domain.net/wp-content/themes/twentyten/inc/theme-options.php)[function.require] :无法打开流:第 122 行的 /home/xxx/public_html/domain.net/wp-content/themes/twentyeleven/functions.php 中没有这样的文件或目录致命错误:require() [function.require]:失败打开所需的 '/home/xxx/public_html/domain.net/wp-content/themes/twentyten/inc/theme-options.php' (include_path='.:/usr/lib/php:/usr/local/lib/ php') 在 /home/xxx/public_html/domain.net/wp-content/themes/twentyeleven/functions.php 第 122 行
【解决方案2】:

在 wordpress 根目录中,将 require( ABSPATH . WPINC . '/plugin.php' ); 放在文件 wp-settings.php 中的 require( ABSPATH . WPINC . '/functions.php' ); 之前。 我在http://wordpress.org/support/topic/fatal-error-call-to-undefined-function-add_filter 验证了这个解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2016-12-11
    • 2010-11-03
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多