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

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”

Read More

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

Related posts

Leave a Reply

2 comments