Fatal error: Call to undefined function add_filter() in index.php in Wordpress

倖福魔咒の 提交于 2019-12-11 18:01:14

问题


I want to change my wordpress theme dynamically by user browser. So I found this code on the net and added it to my index.php file

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

After that it shows me "Fatal error: Call to undefined function add_filter() in /home/xxx/public_html/domain.com/index.php on line 17"

As I undertand the "add_filter()" should be a function that is built in wordpress.


回答1:


As Dawson said, this needs to go in your /wp-content/themes/[theme]/functions.php file.




回答2:


In wordpress root directory, put require( ABSPATH . WPINC . '/plugin.php' ); before require( ABSPATH . WPINC . '/functions.php' ); in file wp-settings.php. I verified this solution at http://wordpress.org/support/topic/fatal-error-call-to-undefined-function-add_filter.



来源:https://stackoverflow.com/questions/13276583/fatal-error-call-to-undefined-function-add-filter-in-index-php-in-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!