Неустранимая ошибка: вызов неопределенной функции add_filter() в index.php в Wordpress

Я хочу динамически менять тему 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;
}

После этого он показывает мне «Неустранимая ошибка: вызов неопределенной функции add_filter() в /home/xxx/public_html/domain.com/index.php в строке 17»

Насколько я понимаю, «add_filter()» должна быть функцией, встроенной в wordpress.


person Karuvägistaja    schedule 07.11.2012    source источник
comment
попробуйте добавить это в functions.php вместо index.php   -  person Dawson Loudon    schedule 07.11.2012
comment
Дело в том, что он должен быть прочитан сайтом в первую очередь, чтобы он знал, какой шаблон загрузить.   -  person Karuvägistaja    schedule 08.11.2012


Ответы (2)


Как сказал Доусон, это должно быть в вашем файле /wp-content/themes/[theme]/functions.php.

person Michael Lewis    schedule 07.11.2012
comment
хорошо, теперь он говорит: Предупреждение: требуется (/home/xxx/public_html/domain.net/wp-content/themes/twentyten/inc/theme-options.php) [function.require]: не удалось открыть поток: нет такого файл или каталог в /home/xxx/public_html/domain.net/wp-content/themes/twentyeleven/functions.php в строке 122 Неустранимая ошибка: 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 - person Karuvägistaja; 08.11.2012

В корневом каталоге WordPress поставьте require( ABSPATH . WPINC . '/plugin.php' ); перед require( ABSPATH . WPINC . '/functions.php' ); в файле wp-settings.php. Я проверил это решение на http://wordpress.org/support/topic/fatal-error-call-to-undefined-function-add_filter.

person zzxwill    schedule 12.04.2014