2

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.

2
  • 1
    try adding this to functions.php instead of index.php Commented Nov 7, 2012 at 19:23
  • The thing is, it should be the very first thing to be read by the site so it knows what template to load up. Commented Nov 7, 2012 at 22:37

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

ok, now it says:Warning: require(/home/xxx/public_html/domain.net/wp-content/themes/twentyten/inc/theme-options.php) [function.require]: failed to open stream: No such file or directory in /home/xxx/public_html/domain.net/wp-content/themes/twentyeleven/functions.php on line 122 Fatal error: require() [function.require]: Failed opening required '/home/xxx/public_html/domain.net/wp-content/themes/twentyten/inc/theme-options.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/xxx/public_html/domain.net/wp-content/themes/twentyeleven/functions.php on line 122
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.