我想通过用户浏览器动态更改我的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中内置的函数。
#1
正如道森所说, 这需要放在你的/wp-content/themes/[theme]/functions.php文件中。
#2
在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上验证了此解决方案。
来源:
https://www.srcmini02.com/67247.html